clear all;
clc;
format short g

load ('input1960-2001all.mat');

load ('forecast80_2001.mat');
%-----------------------------------------------------------
%imortant specifications

load ('forecast01Fold.mat'); % which is for SN=50 and 71 years till 2030 in it the Population and Income forecasts(the latter derived form the former) by POP_GDPinterpolation2001onward.m
                                
SN=50; %state numbers (here we do work without DC)
YEARS=42; %making number of years a variable

%base settings for the regressions
theta=10; %how many years out
N=10; %representing the number of predictions we make with theta years out for which we then calculate the MSFE 
R=YEARS-theta-N; % period we start from to estimate theta years out (compared to older writing not the 1st prediction period)
Rr=YEARS; %last period in sample

YEARSFOR=29; % how many years we want to have the real out of sample forecast for each moddel  ->29 will do till 2030 

TN=3;   %years after 1971 for which the time fixed effects regression will make a structural break each in an extra model specification       
QDN=7;   %number of combinations with the quality dummies---determinds the number of loops where they apply

halftimefilename1='reg_25_half.mat'; % to save the main results at half time as these are often long runs
halftimefilename2='reg_25.mat';
filenameBoot='reg_25_bootvar.mat'; %name for the file saving all important series from the output for a later bootstrap test run

filename_boot='reg_25_best';  % diray for the results of boostrap

l=0;            % l will be a counter for the loops that the x diffrent models go through, we need that number to form the indices of the f_hat matrix etc
Cc=2; %income scenario to start looping through - from 3 different income forecast (2 is the middle (base) scenario - the only we use in these runs - this is a left over from a much earlier versions)
CC=2; %income scenario to end looping through of the 3 we have, Note very large Matrix with huge data requirment
SSpl=8; Spl=SSpl; %number of splince we include below
%-----------------------------------------------------------

%1st before stacking get the aggregate Variables
for c=Cc:CC
IncomeFA{c}=(IncomeF{c}(51,YEARS+1:YEARS+YEARSFOR))';
IncomepcFA{c}=(IncomepcF{c}(51,YEARS+1:YEARS+YEARSFOR))';
end

incomeFA=IncomeFA;
incomepcFA=IncomepcFA;

popallFA=(popallF(51,YEARS+1:YEARS+YEARSFOR))';
popdensFA=(popdensF(51,YEARS+1:YEARS+YEARSFOR))';
PopallFA=popallFA;
PopdensFA=popdensFA;

%2st stacking the Forecasts

for c=Cc:CC
for i=YEARS+1:YEARS+YEARSFOR 
    x(1+SN*(i-1-YEARS):SN*(i-YEARS))=IncomeF{c}(1:SN,i); 
end
IncomeF{c}=x';

for i=YEARS+1:YEARS+YEARSFOR 
    x(1+SN*(i-1-YEARS):SN*(i-YEARS))=IncomepcF{c}(1:SN,i); 
end
IncomepcF{c}=x';
end

incomepcF=IncomepcF;

for i=YEARS+1:YEARS+YEARSFOR
    x(1+SN*(i-1-YEARS):SN*(i-YEARS))=popdensF(1:SN,i); 
end
popdensF=x';

PopdensF=popdensF;

for i=YEARS+1:YEARS+YEARSFOR 
    x(1+SN*(i-1-YEARS):SN*(i-YEARS))=popallF(1:SN,i); 
end
popallF=x';

CO2=co2;
Co2pc=co2pc;
Incomepc=incomepc;
Popdens=popdens;
Pop=pop;

%since Vars is allready stacked but we need some unstacked in the last
%dataset in order to get all the agg variables for the extra regressions
for i=1:YEARS
    temp(:,i)=CO2(SN*(i-1)+1:SN*(i));
end
temp(51,:)=sum(temp);
co2A=(temp(51,:))';
clear temp;
Co2A=co2A;

for i=1:YEARS
    temp(:,i)=pop(SN*(i-1)+1:SN*(i));
end
temp(51,:)=sum(temp);
popA=(temp(51,:))';
clear temp;
PopA=popA;

for i=1:YEARS
    temp(:,i)=income(SN*(i-1)+1:SN*(i));
end
temp(51,:)=sum(temp);
incomeA=(temp(51,:))';
clear temp;
IncomeA=incomeA;


incomepcA=incomeA./popA;
IncomepcA=incomepcA;    
co2pcA=co2A./popA;
Co2pcA=co2pcA;

popdensA=popA/sum(area50(2051:2100))*1000000;
PopdensA=popdensA;

%now before we stack the predictions from the 80ies we will get predicted
%agg variables from them first.

popforecast80A=(sum(popforecast80))';
Popforecast80A=popforecast80A;

incomeforecast80A=(sum(incomeforecast80))';
Incomeforecast80A=incomeforecast80A;

incomepcforecast80A=incomeforecast80A./popforecast80A;
Incomepcforecast80A=incomepcforecast80A;

popdensforecast80A=popforecast80A/sum(area50(2051:2100))*1000000;
Popdensforecast80A=popdensforecast80A;

% now we can stack the variables for all state level forecast
for i=1:YEARS 
    x(1+SN*(i-1):SN*(i))=incomepcforecast80(:,i);
end;
incomepcforecast80=x';
Incomepcforecast80=x';

for i=1:YEARS 
    x(1+SN*(i-1):SN*(i))=popdensforecast80(:,i);
end;
popdensforecast80=x';
Popdensforecast80=x';

for i=1:YEARS 
    x(1+SN*(i-1):SN*(i))=popforecast80(:,i);
end;
popforecast80=x';
Popforecast80=x';


%country fixed effects
x=ones(YEARS,1);
state=kron(x,eye(SN,SN));
State=state;

%generate quality dummies
QD{1}=coastal;       %coastal
QD{2}=coal;      %coal
QD{3}=oil;       % oil+gas
QD{4}=[coastal,coal];   %coastal, coal 
QD{5}=[coastal,oil];   %coastal, oil+gas
QD{6}=[coal,oil];   %coal, oil+gas
QD{7}=[coastal, coal, oil];            %all tree: coastal, coal, oil+gas

% time fixed effects
x=ones(SN,1);
time=kron(eye(YEARS,YEARS),x);
Time=time;

%generate TIME TREND 1 through 42
x=linspace(1,YEARS+YEARSFOR,YEARS+YEARSFOR)';
timetrend=kron(x,ones(SN,1));
LOGtimetrend=log(timetrend);

% and a timetrend for the agg models
timetrendA=linspace(1,YEARS+YEARSFOR,YEARS+YEARSFOR)';
LOGtimetrendA=log(timetrendA);

Crisis1=crisis1;
Crisis2=crisis2;
Crisis3=crisis3;

%now crisis dummies for the agg time series
for i=1:YEARS
    temp(:,i)=Crisis1(SN*(i-1)+1:SN*(i));
end
crisis1A=(temp(1,:))';
clear temp;
Crisis1A=crisis1A;

for i=1:YEARS
    temp(:,i)=Crisis2(SN*(i-1)+1:SN*(i));
end
crisis2A=(temp(1,:))';
clear temp;
Crisis2A=crisis2A;

for i=1:YEARS
    temp(:,i)=Crisis3(SN*(i-1)+1:SN*(i));
end
crisis3A=(temp(1,:))';
clear temp;
Crisis3A=crisis3A;

%-------------------------------------------------------------------------

    
%Benchmark
for r=1:N %big over all loop to go through N regressions and predictions for
    
    %adjusting the data for each loop 
    
    time_r=time(1:SN*(R+r),2:R+r);    %-2 b/c we did cut out the first row to not have a singulare matrix
    state_r=state(1:SN*(R+r),:);
    co2pc_r=co2pc(1:SN*(R+r),:);
    CO2_r=CO2(1:SN*(R+r),:);
    incomepc_r=incomepc(1:SN*(R+r),:);
    popdens_r=popdens(1:SN*(R+r),:);
    pop_r=pop(1:SN*(R+r),:);
    incomepc_rforecast80=incomepcforecast80(1:SN*(R+r),:);
    popdens_rforecast80=popdensforecast80(1:SN*(R+r),:);
    pop_rforecast80=popforecast80(1:SN*(R+r),:);
    
%BENCHMARK model Schmalensee:
    
    Y=log(co2pc_r);
    %constructing the dummies for the 10 (S) segmented income spline
    S=10;
    sortedincome=sort(log(incomepc_r));
    n=size(incomepc_r,1);
    p=ceil(n/S);
    for i=2:S   %we need the separtation points inbetween the groups
        boundary(i)=sortedincome((i-1)*p);
    end;
    boundary(S+1)=inf;
    boundary(1)=0;
    splinedummy=zeros(n,10);
    
   
    for i=1:S
        for j=1:n
            if ((log(incomepc(j))>=boundary(i)) & (log(incomepc(j))<boundary(i+1)))
                splinedummy(j,i)=1;
            end;
        end;
    end;
    
    incomespline=splinedummy.*repmat(log(incomepc_r),1,S);
    
    X_b=[state_r time_r incomespline];
    [nu,ka]=size(X_b);
    [b_bench,a,res,int,stat]=regress(Y,X_b);
    
   
    %regression to determine a time trend from the time fixed effects
    k=size(b_bench,1);
    years=10; %year after 1960 that should have the structural change,  so 12 is 1972
    clear n;
    
    x_time=[ones(R+r-1,1) linspace(1961,1961+R+r-2,R+r-1)' [zeros(years,1)' linspace(0,R+r-2-years,R+r-1-years)]'];
    bt=regress(b_bench(k-S-(R+r-2):k-S),x_time);
    
   
    %Forecasting theta periods into the future and get the deviation
    %from the actual value
    
    %first we need to find the appropriate spline coefficient for the last
    %observed income measure in each state
    
    for j=1:SN
        for i=1:S
            if ((log(incomepcforecast80(SN*(R+r+theta-1)+j))>=boundary(i)) & (log(incomepcforecast80(SN*(R+r+theta-1)+j))<boundary(i+1)))
                income_b(j,1)=b_bench(SN+R+r-1+i);
            end;
        end;
    end;
    
    y=log(co2pc(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN));
    y_hat=[eye(SN,SN)*b_bench(1:SN)+ones(SN,1).*[(R+r-2-years+theta)*bt(3)+bt(1)+(1961+R+r-2+theta)*bt(2)]+log(incomepcforecast80(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN)).*income_b];
    clear bt;


    f_bench(r,1)=ones(SN,1)'*[(exp(y)-exp(y_hat+(res'*res)/(nu-ka)/2)).^2];
    f_benchA(r,1)=ones(SN,1)'*[((exp(y).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta)))-(exp(y_hat+(res'*res)/(nu-ka)/2).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta)))).^2];
    
    
    clear res nu ka 
    
end;



%BENCHMARK Yang % Schneider
% we have prepared growth values for population, incomePC, and their
% baseline carbon intesty and energy intensity, such that we can multipy
% these (done in the yangschneidergth80factor in the POP_GDPinterpolation80new2001.m) and so find the forecast for
% every period.

for r=1:N
    y=(CO2(SN*(R+r-1)+1:SN*(R+r-1)+SN));
    for oh=1:theta
   y=y.*yangschneidergth80factor(:,(R-21+r+oh-1));
    end
    y_hat=y;
   
   y_m=(CO2(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN));
   f_B3(r,1)=ones(SN,1)'*[((y_m./pop(SN*(R+r+theta-1)+1:SN*(R+r+theta)))-(y_hat./pop(SN*(R+r+theta-1)+1:SN*(R+r+theta)))).^2];  %per capita cirteria
   f_B3A(r,1)=ones(SN,1)'*[((y_m)-(y_hat)).^2]; % as orginally, the aggregate criteria
    
end

clear X_b Y incomepc_r S p n boundary splinedummy sortedincome incomespline
%--------------------------------------------------------------------------
tic   %change
for unc=1:2 % extra overall loop to calculate the unconditional forecasts, will do simply by changing data input into forecast variables leaving the rest of the code "almost" alone

    if unc==2
        R=YEARS-theta-N-theta; % resetting the R (base sample regression periods) but substracting theta as we will lag income and population densitiy in this second overall loop and therefore reduce the sample size 
           Rr=YEARS-theta; %total smaple size reduced by lag size (which is the same as the forecast horizon) 
           siz=YEARS*SN;
           siz2=(YEARS-theta)*SN;
           
           YEARSFOR=theta; % that is since we can only forcast as many years out of sample as the we cut off/lag the LHS variables by --- beforehand due to iteration forecast of Co2 and predictions of income and population we coudl go out to2030
           
           for c=Cc:CC
            IncomepcF{c}=[Incomepc(siz-(theta*SN)+1:siz);IncomepcF{c}(1:(YEARSFOR-theta)*SN)]; %change forecast to essentially be lagged sample variables
           end
           incomepcF=IncomepcF;
           
           PopdensF=[Popdens(siz-(theta*SN)+1:siz);PopdensF(1:(YEARSFOR-theta)*SN)]; 
           popdensF=PopdensF;
           
           Popdens=Popdens(1:siz2); %cutting off last periods and make smaple smaller those periods will be the once now used for forecasting (given the lag structure)
           popdens=Popdens;
           Incomepc=Incomepc(1:siz2);
           incomepc=Incomepc;
           
           Incomepcforecast80=Incomepc; %the out of sample experiment forecast are now just real income and popdens lagged 
           incomepcforecast80=Incomepcforecast80;
           Popdensforecast80=Popdens;
           popdensforecast80=Popdensforecast80;
           
           co2pc=(Co2pc((SN*theta)+1:siz)); % we will have to cut off first theta=10 years of the dependent variable as we use laggs of explainatory vars 
           co2pcL=(Co2pc(1:siz2)); % for the co2pc as LHS lagged variable we need to define the variable from start to 10 years before the end
           
           Co2pc=co2pc;
           Co2pcL=co2pcL;
           
           
           Pop=Pop((SN*theta)+1:siz); 
           pop=Pop;
           
           Crisis1=Crisis1((SN*theta)+1:siz);
           Crisis2=Crisis2((SN*theta)+1:siz);
           Crisis3=Crisis3((SN*theta)+1:siz);
            crisis1=Crisis1;
            crisis2=Crisis2;
            crisis3=Crisis3;
           
           Time=Time(((SN*theta)+1:siz),1+theta:Rr+theta);      
           time=Time;
           State=State(((SN*theta)+1:siz),:);
           state=State;
           
           %VARIABLES FOR US AGG REGRESSIONS
                                    siz2=(YEARS-theta);
                                  for c=Cc:CC
                                    IncomepcFA{c}=[IncomepcA(YEARS-(theta)+1:YEARS);IncomepcFA{c}(1:(YEARSFOR-theta))]; %change forecast to essentially be lagged sample variables
                                   end
                                   incomepcFA=IncomepcFA;

                                   PopdensFA=[PopdensA(YEARS-(theta)+1:YEARS);PopdensFA(1:(YEARSFOR-theta))]; 
                                   popdensFA=PopdensFA;

                                   PopdensA=PopdensA(1:siz2); %cutting off last periods and make smaple smaller those periods will be the once now used for forecasting (given the lag structure)
                                   popdensA=PopdensA;
                                   IncomepcA=IncomepcA(1:siz2);
                                   incomepcA=IncomepcA;

                                   Incomepcforecast80A=IncomepcA; %the out of sample experiment forecast are now just real income and popdens lagged 
                                   incomepcforecast80A=Incomepcforecast80A;
                                   Popdensforecast80A=PopdensA;
                                   popdensforecast80A=Popdensforecast80A;

                                   co2pcA=(Co2pcA((theta)+1:YEARS)); % we will have to cut off first theta=10 years of the dependent variable as we use laggs of explainatory vars 
                                   co2pcAL=(Co2pcA(1:YEARS-theta)); % for LHS lagged co2pc inclusion 
                                   
                                   Co2pcA=co2pcA;
                                   Co2pcAL=co2pcAL;
                                   
                                   
                                   PopA=PopA((theta)+1:YEARS); 
                                   popA=PopA;

                                   Crisis1A=Crisis1A((theta)+1:YEARS);
                                   Crisis2A=Crisis2A((theta)+1:YEARS);
                                   Crisis3A=Crisis3A((theta)+1:YEARS);
                                    crisis1A=Crisis1A;
                                    crisis2A=Crisis2A;
                                    crisis3A=Crisis3A;
           
    clear incomespline_ income_dummy z S
    Spl=SSpl;
    end

    
incomespline_=cell(N+1,10,2,2);
income_dummy=cell(N+YEARSFOR,10,2,2);

%generating the spline for the income data for S=3,5,7,8,10 to be used as possible model types 
% no need for u=1:2 loop to generate the spline dummies for the unconditional forecasts as this is part of the overall loop
for z=1:2 % to have it in LAGed form as well (z=2)
    for S=(10-Spl+1):10
        clear incomepc_r p n boundary splinedummy sortedincome incomespline
        for r=1:N
            
            %adjusting the data for each loop 
            
            incomepc_r=incomepc(1:SN*(R+r),:);
            if z==2 && unc==1 % in the Uncodictional loop we don't need an extra lag all is lag by theta from the start and no difference in the co2 lag regressions. and we are not run income-splines as models with RHS lag regeressions
                incomepc_r=incomepc(SN+1:SN*(R+r),:);
            end;
            
            %constructing the dummies for the 10 (S) segmented income spline
            
            sortedincome=sort(log(incomepc_r));
            n=size(incomepc_r,1);
            p=ceil(n/S);
            for i=2:S   %we need the separtation points inbetween the groups
                boundary(i)=sortedincome((i-1)*p);
            end;
            boundary(S+1)=inf;
            boundary(1)=0;
            splinedummy=zeros(n,S);
            

            for i=1:S
                for j=1:n
                    if ((log(incomepc_r(j))>=boundary(i)) && (log(incomepc_r(j))<boundary(i+1)))
                        splinedummy(j,i)=1;
                    end;
                end;
            end;
            
            incomespline=splinedummy.*repmat(log(incomepc_r),1,S);
            
            incomespline_{r,S,2,z}=incomespline;
            
            income_dummy{r,S,2,z}=zeros(SN,S);
            for j=1:SN
                for i=1:S
                    if ((log(incomepcforecast80(SN*(R+r+theta-1)+j))>=boundary(i)) && (log(incomepcforecast80(SN*(R+r+theta-1)+j))<boundary(i+1)))
                        income_dummy{r,S,2,z}(j,i)=1;
                    end;
                end;
            end;
   %-------------------doing it for full sample 
    
   if r==N
   incomepc_r=incomepc(1:SN*(Rr),:);
            if z==2 && unc==1
                incomepc_r=incomepc(SN+1:SN*(Rr),:);
            end;
            
            %constructing the dummies for the 10 (S) segmented income spline
            
            sortedincome=sort(log(incomepc_r));
            n=size(incomepc_r,1);
            p=ceil(n/S);
            for i=2:S   %we need the separtation points inbetween the groups
                boundary(i)=sortedincome((i-1)*p);
            end;
            boundary(S+1)=inf;
            boundary(1)=0;
            splinedummy=zeros(n,S);

            for i=1:S
                for j=1:n
                    if ((log(incomepc_r(j))>=boundary(i)) && (log(incomepc_r(j))<boundary(i+1)))
                        splinedummy(j,i)=1;
                    end;
                end;
            end;
            
            incomespline=splinedummy.*repmat(log(incomepc_r),1,S);
            
            incomespline_{N+1,S,2,z}=incomespline;
            
            
            for c=Cc:CC
            for YF=1:YEARSFOR; 
                income_dummy{(N+YF),S,2,c}=zeros(SN,S);
            for j=1:SN
                for i=1:S
                    if ((log(IncomepcF{c}(SN*(YF-1)+j))>=boundary(i)) && (log(IncomepcF{c}(SN*(YF-1)+j))<boundary(i+1)))
                        income_dummy{(N+YF),S,2,c}(j,i)=1;
                     
                    end;
                end;
            end;
            end
            end
   end  
            
   %---------------------------         
        end;
    end;
    
    %the same thing once again to get the splines for the non-log incomes
    for S=(10-Spl+1):10
        clear incomepc_r p n boundary splinedummy sortedincome incomespline
        for r=1:N
            
            %adjusting the data for each loop 
            
            incomepc_r=incomepc(1:SN*(R+r),:);
            if z==2 && unc==1
                incomepc_r=incomepc(SN+1:SN*(R+r),:);
            end;
            %constructing the dummies for the 10 (S) segmented income spline
            
            sortedincome=sort((incomepc_r));
            n=size(incomepc_r,1);
            p=ceil(n/S);
            for i=2:S   %we need the separtation points inbetween the groups
                boundary(i)=sortedincome((i-1)*p);
            end;
            boundary(S+1)=inf;
            boundary(1)=0;
            splinedummy=zeros(n,S);
                 
            for i=1:S
                for j=1:n
                    if (((incomepc_r(j))>=boundary(i)) && ((incomepc_r(j))<boundary(i+1)))
                        splinedummy(j,i)=1;
                    end;
                end;
            end;
            
            incomespline=splinedummy.*repmat((incomepc_r),1,S);
            
            incomespline_{r,S,1,z}=incomespline;
            
            
            income_dummy{r,S,1,z}=zeros(SN,S);
            for j=1:SN
                for i=1:S
                    if (((incomepcforecast80(SN*(R+r+theta-1)+j))>=boundary(i)) && ((incomepcforecast80(SN*(R+r+theta-1)+j))<boundary(i+1)))
                        income_dummy{r,S,1,z}(j,i)=1;
                    end;
                end;
            end;
%-------------------doing it for full sample 
    
   if r==N
   incomepc_r=incomepc(1:SN*(Rr),:);
            if z==2 && unc==1
                incomepc_r=incomepc(SN+1:SN*(Rr),:);
            end;
            
            %constructing the dummies for the 10 (S) segmented income spline
            
            sortedincome=sort((incomepc_r));
            n=size(incomepc_r,1);
            p=ceil(n/S);
            for i=2:S   %we need the separtation points inbetween the groups
                boundary(i)=sortedincome((i-1)*p);
            end;
            boundary(S+1)=inf;
            boundary(1)=0;
            splinedummy=zeros(n,S);
            
            
            for i=1:S
                for j=1:n
                    if (((incomepc_r(j))>=boundary(i)) & ((incomepc_r(j))<boundary(i+1)))
                        splinedummy(j,i)=1;
                    end;
                end;
            end;
            
            incomespline=splinedummy.*repmat((incomepc_r),1,S);
            
            incomespline_{N+1,S,1,z}=incomespline;
            
            
            for c=Cc:CC
            for YF=1:YEARSFOR; 
                income_dummy{(N+YF),S,1,c}=zeros(SN,S);
            for j=1:SN
                for i=1:S
                    if (((IncomepcF{c}(SN*(YF-1)+j))>=boundary(i)) && ((IncomepcF{c}(SN*(YF-1)+j))<boundary(i+1)))
                        income_dummy{(N+YF),S,1,c}(j,i)=1;
                    end;
                end;
            end;
            end
            end
   end  
            
   %---------------------------  
        end;
    end;
end;




for lag=1:2    % letting all the models rune once as they are and once with LHS CO2 Lags as a RHS varaible
   
    L=4;
   
    for logs=1:L        % we do 4 big lops (cases) where the variables are in logs (2,4) and one without logs Nr. 1 and 3 and where 1,2 are without Laged RHS variables and 3,4 are.

        if logs==2  && lag==1
            
            
            incomepc=log(Incomepc);
            popdens=log(Popdens);
            
            incomepcforecast80=log(Incomepcforecast80);
            popdensforecast80=log(Popdensforecast80);
            
            for c=Cc:CC
            incomepcF{c}=log(IncomepcF{c});
            end
            popdensF=log(PopdensF);         
            
            co2pc=log(Co2pc);
            
                              incomepcA=log(IncomepcA);
                            popdensA=log(PopdensA);

                            incomepcforecast80A=log(Incomepcforecast80A);
                            popdensforecast80A=log(Popdensforecast80A);

                            for c=Cc:CC
                            incomepcFA{c}=log(IncomepcFA{c});
                            end
                            popdensFA=log(PopdensFA);         

                            co2pcA=log(Co2pcA);
                            
        elseif logs==3  && lag==1    %loop for the lags 
        
        Rr=Rr-1;
            siz=size(Co2pc,1);
            R=R-1;
            LAGincomepc=(Incomepc(1:siz-SN));
            LAGpopdens=(Popdens(1:siz-SN));
            incomepc=(Incomepc(SN+1:siz));
            popdens=(Popdens(SN+1:siz));
            
            LAGincomepcforecast80=(Incomepcforecast80(1:siz-SN));
            LAGpopdensforecast80=(Popdensforecast80(1:siz-SN));
            incomepcforecast80=(Incomepcforecast80(SN+1:siz));
            popdensforecast80=(Popdensforecast80(SN+1:siz));
            
            for c=Cc:CC
            LAGincomepcF{c}=[Incomepc(siz-SN+1:siz);IncomepcF{c}(1:(YEARSFOR-1)*SN)];            
            end
            incomepcF=(IncomepcF);
            LAGpopdensF=[Popdens(siz-SN+1:siz);PopdensF(1:(YEARSFOR-1)*SN)]; 
            popdensF=(PopdensF);  
            
            crisis1=Crisis1(SN+1:siz);
            crisis2=Crisis2(SN+1:siz);
            crisis3=Crisis3(SN+1:siz);
            
            co2pc=(Co2pc(SN+1:siz));
            pop=Pop(SN+1:siz);              
            
            time=Time((SN+1:siz),2:Rr+1);    
            state=State((SN+1:siz),:);
            
            sizA=size(Co2pcA,1);
                                    LAGincomepcA=(IncomepcA(1:sizA-1));
                                    LAGpopdensA=(PopdensA(1:sizA-1));
                                    incomepcA=(IncomepcA(2:sizA));
                                    popdensA=(PopdensA(2:sizA));

                                    LAGincomepcforecast80A=(Incomepcforecast80A(1:sizA-1));
                                    LAGpopdensforecast80A=(Popdensforecast80A(1:sizA-1));
                                    incomepcforecast80A=(Incomepcforecast80A(2:sizA));
                                    popdensforecast80A=(Popdensforecast80A(2:sizA));

                                    for c=Cc:CC
                                    LAGincomepcFA{c}=[IncomepcA(sizA);IncomepcFA{c}(1:(YEARSFOR-1))];            
                                    end
                                    incomepcFA=(IncomepcFA);
                                    LAGpopdensFA=[PopdensA(Rr);PopdensFA(1:(YEARSFOR-1))]; 
                                    popdensFA=(PopdensFA);  

                                    crisis1A=Crisis1A(2:sizA);
                                    crisis2A=Crisis2A(2:sizA);
                                    crisis3A=Crisis3A(2:sizA);

                                    co2pcA=(Co2pcA(2:sizA));
                                    popA=PopA(2:sizA); 
            
            
            
        elseif logs==4  && lag==1   %loop for the logs of lags
            
            LAGincomepc=log(Incomepc(1:siz-SN));
            LAGpopdens=log(Popdens(1:siz-SN));
            incomepc=log(Incomepc(SN+1:siz));
            popdens=log(Popdens(SN+1:siz));

            LAGincomepcforecast80=log(Incomepcforecast80(1:siz-SN));
            LAGpopdensforecast80=log(Popdensforecast80(1:siz-SN));
            incomepcforecast80=log(Incomepcforecast80(SN+1:siz));
            popdensforecast80=log(Popdensforecast80(SN+1:siz));
            
            for c=Cc:CC
            LAGincomepcF{c}=log([Incomepc(siz-SN+1:siz);IncomepcF{c}(1:(YEARSFOR-1)*SN)]);
            incomepcF{c}=log(IncomepcF{c});
            end
            LAGpopdensF=log([Popdens(siz-SN+1:siz);PopdensF(1:(YEARSFOR-1)*SN)]); 
            popdensF=log(PopdensF); 
            
            co2pc=log(Co2pc(SN+1:siz));
            
            time=Time((SN+1:siz),2:Rr+1);     
            state=State((SN+1:siz),:);
            
            
                                   LAGincomepcA=log(IncomepcA(1:sizA-1));
                                LAGpopdensA=log(PopdensA(1:sizA-1));
                                incomepcA=log(IncomepcA(2:sizA));
                                popdensA=log(PopdensA(2:sizA));

                                LAGincomepcforecast80A=log(Incomepcforecast80A(1:sizA-1));
                                LAGpopdensforecast80A=log(Popdensforecast80A(1:sizA-1));
                                incomepcforecast80A=log(Incomepcforecast80A(2:sizA));
                                popdensforecast80A=log(Popdensforecast80A(2:sizA));

                                for c=Cc:CC
                                LAGincomepcFA{c}=log([IncomepcA(sizA);IncomepcFA{c}(1:(YEARSFOR-1))]);
                                incomepcFA{c}=log(IncomepcFA{c});
                                end
                                LAGpopdensFA=log([PopdensA(sizA);PopdensFA(1:(YEARSFOR-1))]); 
                                popdensFA=log(PopdensFA); 

                                co2pcA=log(Co2pcA(2:sizA));

            
            
        elseif logs==1 && lag==2         %when we enter the second over all loop with CO2 lags it sets all back to the start 
          
          if unc==1
            siz=size(Co2pc,1);
            incomepc=(Incomepc(SN+1:siz));
            popdens=(Popdens(SN+1:siz));
            
            incomepcforecast80=(Incomepcforecast80(SN+1:siz));
            popdensforecast80=(Popdensforecast80(SN+1:siz));
            
            incomepcF=(IncomepcF);
            popdensF=(PopdensF); 
            
             co2pc=(Co2pc(SN+1:siz));
            time=Time((SN+1:siz),2:Rr+1);     
            state=State((SN+1:siz),:);
            for i=1:((siz-SN)/SN)
                LAGco2pc((i-1)*SN+1:i*SN,:)=diag(Co2pc((i-1)*SN+1:i*SN));
            end
         
                                        sizA=size(Co2pcA,1);
                                        incomepcA=(IncomepcA(2:sizA));
                                        popdensA=(PopdensA(2:sizA));

                                        incomepcforecast80A=(Incomepcforecast80A(2:sizA));
                                        popdensforecast80A=(Popdensforecast80A(2:sizA));

                                        incomepcFA=(IncomepcFA);
                                        popdensFA=(PopdensFA); 

                                        co2pcA=(Co2pcA(2:sizA));
                                        
                                        LAGco2pcA=Co2pcA(1:sizA-1);
          elseif unc==2  
                          Rr=YEARS-theta; % in the unconditional loop we take the extra year cut off on top of the 10 years back for as long as we don't have the lagged LHS variables in there
                          R=YEARS-theta-N-theta;
                        siz=size(Co2pc,1);
                        incomepc=(Incomepc(1:siz));
                        popdens=(Popdens(1:siz));

                        incomepcforecast80=(Incomepcforecast80(1:siz));
                        popdensforecast80=(Popdensforecast80(1:siz));

                        incomepcF=(IncomepcF);
                        popdensF=(PopdensF); 

                        
                         crisis1=Crisis1(1:siz);
                        crisis2=Crisis2(1:siz);
                        crisis3=Crisis3(1:siz);
                        
                         co2pc=(Co2pc(1:siz));
                         pop=Pop(1:siz); 
                        time=Time((1:siz),1:Rr);     
                        state=State((1:siz),:);
                        for i=1:((siz)/SN)
                            LAGco2pc((i-1)*SN+1:i*SN,:)=diag(Co2pcL((i-1)*SN+1:i*SN));
                        end


                                                    sizA=size(Co2pcA,1);
                                                    incomepcA=(IncomepcA(1:sizA));
                                                    popdensA=(PopdensA(1:sizA));

                                                    incomepcforecast80A=(Incomepcforecast80A(1:sizA));
                                                    popdensforecast80A=(Popdensforecast80A(1:sizA));

                                                    incomepcFA=(IncomepcFA);
                                                    popdensFA=(PopdensFA); 

                                                    co2pcA=(Co2pcA(1:sizA));
                                                    crisis1A=Crisis1A(1:sizA);
                                                    crisis2A=Crisis2A(1:sizA);
                                                    crisis3A=Crisis3A(1:sizA);

                                                    
                                                    popA=PopA(1:sizA); 

                                                    LAGco2pcA=Co2pcAL(1:sizA);
          
          end
          


                                        
            
            
            
        elseif logs==2  && lag==2
          if unc==1  
            siz=size(Co2pc,1);
            for i=1:((siz-SN)/SN)
                LAGco2pc((i-1)*SN+1:i*SN,:)=diag(log(Co2pc((i-1)*SN+1:i*SN)));
            end
            incomepc=log(Incomepc(SN+1:siz));
            popdens=log(Popdens(SN+1:siz));
            
            incomepcforecast80=log(Incomepcforecast80(SN+1:siz));
            popdensforecast80=log(Popdensforecast80(SN+1:siz));
            
            for c=Cc:CC
            incomepcF{c}=log(IncomepcF{c});
            end
            popdensF=log(PopdensF); 
            
            co2pc=log(Co2pc(SN+1:siz));
            time=Time((SN+1:siz),2:Rr+1);    
            state=State((SN+1:siz),:);
                                    sizA=size(Co2pcA,1);
                                    LAGco2pcA=log(Co2pcA(1:sizA-1));
                                    incomepcA=log(IncomepcA(2:sizA));
                                    popdensA=log(PopdensA(2:sizA));

                                    incomepcforecast80A=log(Incomepcforecast80A(2:sizA));
                                    popdensforecast80A=log(Popdensforecast80A(2:sizA));

                                    for c=Cc:CC
                                    incomepcFA{c}=log(IncomepcFA{c});
                                    end
                                    popdensFA=log(PopdensFA); 

                                    co2pcA=log(Co2pcA(2:sizA));
           elseif unc==2 
                                                     
                            siz=size(Co2pc,1);
                            for i=1:((siz)/SN)
                                LAGco2pc((i-1)*SN+1:i*SN,:)=diag(log(Co2pcL((i-1)*SN+1:i*SN)));
                            end
                            incomepc=log(Incomepc(1:siz));
                            popdens=log(Popdens(1:siz));

                            incomepcforecast80=log(Incomepcforecast80(1:siz));
                            popdensforecast80=log(Popdensforecast80(1:siz));

                            for c=Cc:CC
                            incomepcF{c}=log(IncomepcF{c});
                            end
                            popdensF=log(PopdensF); 

                            co2pc=log(Co2pc(1:siz));
                            time=Time((1:siz),1:Rr);    
                            state=State((1:siz),:);
                                                    sizA=size(Co2pcA,1);
                                                    LAGco2pcA=log(Co2pcAL(1:sizA));
                                                    incomepcA=log(IncomepcA(1:sizA));
                                                    popdensA=log(PopdensA(1:sizA));

                                                    incomepcforecast80A=log(Incomepcforecast80A(1:sizA));
                                                    popdensforecast80A=log(Popdensforecast80A(1:sizA));

                                                    for c=Cc:CC
                                                    incomepcFA{c}=log(IncomepcFA{c});
                                                    end
                                                    popdensFA=log(PopdensFA); 

                                                    co2pcA=log(Co2pcA(1:sizA));                     
          end
            
        elseif logs==3  && lag==2 
            
            if unc==2
                 Rr=Rr-1; %as we recap again an extra period for the laged LHS varaibles which was not the case for log=1&2 only in the unconditional loop
                 R=R-1;
            end
            siz=size(Co2pc,1);
            LAGincomepc=(Incomepc(1:siz-SN));
            LAGpopdens=(Popdens(1:siz-SN));
           
                    if unc==1
                    for i=1:((siz-SN)/SN)
                        LAGco2pc((i-1)*SN+1:i*SN,:)=diag(Co2pc((i-1)*SN+1:i*SN));
                    end
                    elseif unc==2
                    for i=1:((siz-SN)/SN) %this is not so much laggin as cutting of the first period of the lagged Co2 variable
                        LAGco2pc((i-1)*SN+1:i*SN,:)=diag(Co2pcL((i-1+1)*SN+1:(i+1)*SN));
                    end           
                    end
           
            incomepc=(Incomepc(SN+1:siz));
            popdens=(Popdens(SN+1:siz));
            
            LAGincomepcforecast80=(Incomepcforecast80(1:siz-SN));
            LAGpopdensforecast80=(Popdensforecast80(1:siz-SN));
            incomepcforecast80=(Incomepcforecast80(SN+1:siz));
            popdensforecast80=(Popdensforecast80(SN+1:siz));
            
            for c=Cc:CC
            LAGincomepcF{c}=[Incomepc(siz-SN+1:siz);IncomepcF{c}(1:(YEARSFOR-1)*SN)];            
            end
            incomepcF=(IncomepcF);
            LAGpopdensF=([Popdens(siz-SN+1:siz);PopdensF(1:(YEARSFOR-1)*SN)]); 
            popdensF=(PopdensF);  
            
            
            pop=Pop(SN+1:siz);
            crisis1=Crisis1(SN+1:siz);
            crisis2=Crisis2(SN+1:siz);
            crisis3=Crisis3(SN+1:siz);
            
            co2pc=(Co2pc(SN+1:siz));
            time=Time((SN+1:siz),2:Rr+1);  
            state=State((SN+1:siz),:);
            
                                    sizA=size(Co2pcA,1);
                                    LAGincomepcA=(IncomepcA(1:sizA-1));
                                    LAGpopdensA=(PopdensA(1:sizA-1));
                                    
                                            if unc==1
                                            LAGco2pcA=Co2pcA(1:sizA-1);
                                            elseif unc==2
                                            LAGco2pcA=(Co2pcAL(2:sizA));    
                                            end
                                    incomepcA=(IncomepcA(2:sizA));
                                    popdensA=(PopdensA(2:sizA));

                                    LAGincomepcforecast80A=(Incomepcforecast80A(1:sizA-1));
                                    LAGpopdensforecast80A=(Popdensforecast80A(1:sizA-1));
                                    incomepcforecast80A=(Incomepcforecast80A(2:sizA));
                                    popdensforecast80A=(Popdensforecast80A(2:sizA));

                                    for c=Cc:CC
                                    LAGincomepcFA{c}=[IncomepcA(sizA);IncomepcFA{c}(1:(YEARSFOR-1))];            
                                    end
                                    incomepcFA=(IncomepcFA);
                                    LAGpopdensFA=([PopdensA(sizA);PopdensFA(1:(YEARSFOR-1))]); 
                                    popdensFA=(PopdensFA);   

                                    co2pcA=(Co2pcA(2:sizA));
                                    crisis1A=Crisis1A(2:sizA);
                                    crisis2A=Crisis2A(2:sizA);
                                    crisis3A=Crisis3A(2:sizA);

                                    
                                    popA=PopA(2:sizA); 
            
         
           
            
        elseif logs==4  && lag==2
            
            LAGincomepc=log(LAGincomepc);
            LAGpopdens=log(LAGpopdens);
            
                    if unc==1
                    for i=1:((siz-SN)/SN)
                        LAGco2pc((i-1)*SN+1:i*SN,:)=diag(log(Co2pc((i-1)*SN+1:i*SN)));
                    end
                    elseif unc==2
                    for i=1:((siz-SN)/SN)
                        LAGco2pc((i-1)*SN+1:i*SN,:)=diag(log(Co2pcL((i-1+1)*SN+1:(i+1)*SN)));
                    end            
                    end

            incomepc=log(Incomepc(SN+1:siz));
            popdens=log(Popdens(SN+1:siz));
            
            incomepcforecast80=log(Incomepcforecast80(SN+1:siz));
            popdensforecast80=log(Popdensforecast80(SN+1:siz));
            LAGincomepcforecast80=log(LAGincomepcforecast80);
            LAGpopdensforecast80=log(LAGpopdensforecast80);
            
            for c=Cc:CC
            LAGincomepcF{c}=log([Incomepc(siz-SN+1:siz);IncomepcF{c}(1:(YEARSFOR-1)*SN)]);
            incomepcF{c}=log(IncomepcF{c});
            end
            LAGpopdensF=log([Popdens(siz-SN+1:siz);PopdensF(1:(YEARSFOR-1)*SN)]); 
            popdensF=log(PopdensF);  
            
            co2pc=log(Co2pc(SN+1:siz));
            time=Time((SN+1:siz),2:Rr+1);     
            state=State((SN+1:siz),:);
            
            
                                    LAGincomepcA=log(LAGincomepcA);
                                    LAGpopdensA=log(LAGpopdensA);
                                    
                                    if unc==1
                                    LAGco2pcA=log(Co2pcA(1:sizA-1));
                                    elseif unc==2
                                    LAGco2pcA=log(Co2pcAL(2:sizA));  
                                    end
                                    
                                    incomepcA=log(IncomepcA(2:sizA));
                                    popdensA=log(PopdensA(2:sizA));

                                    incomepcforecast80A=log(Incomepcforecast80A(2:sizA));
                                    popdensforecast80A=log(Popdensforecast80A(2:sizA));
                                    LAGincomepcforecast80A=log(LAGincomepcforecast80A);
                                    LAGpopdensforecast80A=log(LAGpopdensforecast80A);

                                     for c=Cc:CC
                                     LAGincomepcFA{c}=log([IncomepcA(sizA);IncomepcFA{c}(1:(YEARSFOR-1))]);
                                        incomepcFA{c}=log(IncomepcFA{c});
                                     end
                                     LAGpopdensFA=log([PopdensA(sizA);PopdensFA(1:(YEARSFOR-1))]);

                                    popdensFA=log(PopdensFA);  

                                    co2pcA=log(Co2pcA(2:sizA));
          
  
            
        end
        
            % here we can list all possible alternative we can think of in the form below
           
            if logs==1 || logs==2
               
                Spl=SSpl;
                X{1}=[incomepc (incomepc.^2) (incomepc.^3) (incomepc.^4) (incomepc.^5)];
                X{2}=[incomepc (incomepc.^2) (incomepc.^3) (incomepc.^4)];
                X{3}=[incomepc (incomepc.^2) (incomepc.^3)];
                X{4}=[incomepc (incomepc.^2)];
                X{5}=[incomepc];
                X{6}=[incomepc (incomepc.^2) (incomepc.^3) (incomepc.^4) (incomepc.^5) popdens];
                X{7}=[incomepc (incomepc.^2) (incomepc.^3) (incomepc.^4) popdens];
                X{8}=[incomepc (incomepc.^2) (incomepc.^3) popdens];
                X{9}=[incomepc (incomepc.^2) popdens];
                X{10}=[incomepc popdens];
                X{11}=[popdens];
                X{12}=[incomepc (incomepc.^2) (incomepc.^3) (incomepc.^4) (incomepc.^5) popdens (popdens.^2)];
                X{13}=[incomepc (incomepc.^2) (incomepc.^3) (incomepc.^4) popdens (popdens.^2)];
                X{14}=[incomepc (incomepc.^2) (incomepc.^3) popdens (popdens.^2)];
                X{15}=[incomepc (incomepc.^2) popdens (popdens.^2)];
                X{16}=[incomepc popdens (popdens.^2)];
                X{17}=[popdens (popdens.^2)];
                X{18}=[incomepcforecast80];
                X{19}=[incomepcforecast80];
                X{20}=[incomepcforecast80];
                X{21}=[incomepcforecast80];
                X{22}=[incomepcforecast80];         
                X{23}=[incomepcforecast80];
                X{24}=[incomepcforecast80];
                X{25}=[incomepcforecast80]; 
              
                % the part of the forecasted values to do the out of sample
                % model comparisons
               
                Z{1}=[incomepcforecast80 (incomepcforecast80.^2) (incomepcforecast80.^3) (incomepcforecast80.^4) (incomepcforecast80.^5)];
                Z{2}=[incomepcforecast80 (incomepcforecast80.^2) (incomepcforecast80.^3) (incomepcforecast80.^4)];
                Z{3}=[incomepcforecast80 (incomepcforecast80.^2) (incomepcforecast80.^3)];
                Z{4}=[incomepcforecast80 (incomepcforecast80.^2)];
                Z{5}=[incomepcforecast80];
                Z{6}=[incomepcforecast80 (incomepcforecast80.^2) (incomepcforecast80.^3) (incomepcforecast80.^4) (incomepcforecast80.^5) popdensforecast80];
                Z{7}=[incomepcforecast80 (incomepcforecast80.^2) (incomepcforecast80.^3) (incomepcforecast80.^4) popdensforecast80];
                Z{8}=[incomepcforecast80 (incomepcforecast80.^2) (incomepcforecast80.^3) popdensforecast80];
                Z{9}=[incomepcforecast80 (incomepcforecast80.^2) popdensforecast80];
                Z{10}=[incomepcforecast80 popdensforecast80];
                Z{11}=[popdensforecast80];
                Z{12}=[incomepcforecast80 (incomepcforecast80.^2) (incomepcforecast80.^3) (incomepcforecast80.^4) (incomepcforecast80.^5) popdensforecast80 (popdensforecast80.^2)];
                Z{13}=[incomepcforecast80 (incomepcforecast80.^2) (incomepcforecast80.^3) (incomepcforecast80.^4) popdensforecast80 (popdensforecast80.^2)];
                Z{14}=[incomepcforecast80 (incomepcforecast80.^2) (incomepcforecast80.^3) popdensforecast80 (popdensforecast80.^2)];
                Z{15}=[incomepcforecast80 (incomepcforecast80.^2) popdensforecast80 (popdensforecast80.^2)];
                Z{16}=[incomepcforecast80 popdensforecast80 (popdensforecast80.^2)];
                Z{17}=[popdensforecast80 (popdensforecast80.^2)];
            
                % and now for the forcasting with the long data including population and income forcast up to Rr+YF=71 or in case of LAGs Rr+YF=70     
                
                for c=Cc:CC
                Y{1,c}=[incomepcF{c} (incomepcF{c}.^2) (incomepcF{c}.^3) (incomepcF{c}.^4) (incomepcF{c}.^5)];
                Y{2,c}=[incomepcF{c} (incomepcF{c}.^2) (incomepcF{c}.^3) (incomepcF{c}.^4)];
                Y{3,c}=[incomepcF{c} (incomepcF{c}.^2) (incomepcF{c}.^3)];
                Y{4,c}=[incomepcF{c} (incomepcF{c}.^2)];
                Y{5,c}=[incomepcF{c}];
                Y{6,c}=[incomepcF{c} (incomepcF{c}.^2) (incomepcF{c}.^3) (incomepcF{c}.^4) (incomepcF{c}.^5) popdensF];
                Y{7,c}=[incomepcF{c} (incomepcF{c}.^2) (incomepcF{c}.^3) (incomepcF{c}.^4) popdensF];
                Y{8,c}=[incomepcF{c} (incomepcF{c}.^2) (incomepcF{c}.^3) popdensF];
                Y{9,c}=[incomepcF{c} (incomepcF{c}.^2) popdensF];
                Y{10,c}=[incomepcF{c} popdensF];
                Y{11,c}=[popdensF];
                Y{12,c}=[incomepcF{c} (incomepcF{c}.^2) (incomepcF{c}.^3) (incomepcF{c}.^4) (incomepcF{c}.^5) popdensF (popdensF.^2)];
                Y{13,c}=[incomepcF{c} (incomepcF{c}.^2) (incomepcF{c}.^3) (incomepcF{c}.^4) popdensF (popdensF.^2)];
                Y{14,c}=[incomepcF{c} (incomepcF{c}.^2) (incomepcF{c}.^3) popdensF (popdensF.^2)];
                Y{15,c}=[incomepcF{c} (incomepcF{c}.^2) popdensF (popdensF.^2)];
                Y{16,c}=[incomepcF{c} popdensF (popdensF.^2)];
                Y{17,c}=[popdensF (popdensF.^2)];
                Y{18,c}=[incomepcF{c}];
                Y{19,c}=[incomepcF{c}];
                Y{20,c}=[incomepcF{c}];
                Y{21,c}=[incomepcF{c}];
                Y{22,c}=[incomepcF{c}];         
                Y{23,c}=[incomepcF{c}];
                Y{24,c}=[incomepcF{c}];
                Y{25,c}=[incomepcF{c}];
                end


                XA{1}=[incomepcA (incomepcA.^2) (incomepcA.^3) (incomepcA.^4) (incomepcA.^5)];
                XA{2}=[incomepcA (incomepcA.^2) (incomepcA.^3) (incomepcA.^4)];
                XA{3}=[incomepcA (incomepcA.^2) (incomepcA.^3)];
                XA{4}=[incomepcA (incomepcA.^2)];
                XA{5}=[incomepcA];
                XA{6}=[incomepcA (incomepcA.^2) (incomepcA.^3) (incomepcA.^4) (incomepcA.^5) popdensA];
                XA{7}=[incomepcA (incomepcA.^2) (incomepcA.^3) (incomepcA.^4) popdensA];
                XA{8}=[incomepcA (incomepcA.^2) (incomepcA.^3) popdensA];
                XA{9}=[incomepcA (incomepcA.^2) popdensA];
                XA{10}=[incomepcA popdensA];
                XA{11}=[popdensA];
                XA{12}=[incomepcA (incomepcA.^2) (incomepcA.^3) (incomepcA.^4) (incomepcA.^5) popdensA (popdensA.^2)];
                XA{13}=[incomepcA (incomepcA.^2) (incomepcA.^3) (incomepcA.^4) popdensA (popdensA.^2)];
                XA{14}=[incomepcA (incomepcA.^2) (incomepcA.^3) popdensA (popdensA.^2)];
                XA{15}=[incomepcA (incomepcA.^2) popdensA (popdensA.^2)];
                XA{16}=[incomepcA popdensA (popdensA.^2)];
                XA{17}=[popdensA (popdensA.^2)];
                XA{18}=[incomepcforecast80];
                XA{19}=[incomepcforecast80];
                XA{20}=[incomepcforecast80];
                XA{21}=[incomepcforecast80];
                XA{22}=[incomepcforecast80];         
                XA{23}=[incomepcforecast80];
                XA{24}=[incomepcforecast80];
                XA{25}=[incomepcforecast80];
              
                % the part of the forecasted values to do the out of sample
                % model comparisons
               
                ZA{1}=[incomepcforecast80A (incomepcforecast80A.^2) (incomepcforecast80A.^3) (incomepcforecast80A.^4) (incomepcforecast80A.^5)];
                ZA{2}=[incomepcforecast80A (incomepcforecast80A.^2) (incomepcforecast80A.^3) (incomepcforecast80A.^4)];
                ZA{3}=[incomepcforecast80A (incomepcforecast80A.^2) (incomepcforecast80A.^3)];
                ZA{4}=[incomepcforecast80A (incomepcforecast80A.^2)];
                ZA{5}=[incomepcforecast80A];
                ZA{6}=[incomepcforecast80A (incomepcforecast80A.^2) (incomepcforecast80A.^3) (incomepcforecast80A.^4) (incomepcforecast80A.^5) popdensforecast80A];
                ZA{7}=[incomepcforecast80A (incomepcforecast80A.^2) (incomepcforecast80A.^3) (incomepcforecast80A.^4) popdensforecast80A];
                ZA{8}=[incomepcforecast80A (incomepcforecast80A.^2) (incomepcforecast80A.^3) popdensforecast80A];
                ZA{9}=[incomepcforecast80A (incomepcforecast80A.^2) popdensforecast80A];
                ZA{10}=[incomepcforecast80A popdensforecast80A];
                ZA{11}=[popdensforecast80A];
                ZA{12}=[incomepcforecast80A (incomepcforecast80A.^2) (incomepcforecast80A.^3) (incomepcforecast80A.^4) (incomepcforecast80A.^5) popdensforecast80A (popdensforecast80A.^2)];
                ZA{13}=[incomepcforecast80A (incomepcforecast80A.^2) (incomepcforecast80A.^3) (incomepcforecast80A.^4) popdensforecast80A (popdensforecast80A.^2)];
                ZA{14}=[incomepcforecast80A (incomepcforecast80A.^2) (incomepcforecast80A.^3) popdensforecast80A (popdensforecast80A.^2)];
                ZA{15}=[incomepcforecast80A (incomepcforecast80A.^2) popdensforecast80A (popdensforecast80A.^2)];
                ZA{16}=[incomepcforecast80A popdensforecast80A (popdensforecast80A.^2)];
                ZA{17}=[popdensforecast80A (popdensforecast80A.^2)];
                
                
            end;
            %some more with lags
            if logs==3 || logs==4
                clear X Z
                Spl=0;
                X{1}=[incomepc (incomepc.^2) (incomepc.^3) (incomepc.^4) (incomepc.^5) LAGincomepc (LAGincomepc.^2) (LAGincomepc.^3) (LAGincomepc.^4) (LAGincomepc.^5)];
                X{2}=[incomepc (incomepc.^2) (incomepc.^3) (incomepc.^4) LAGincomepc (LAGincomepc.^2) (LAGincomepc.^3) (LAGincomepc.^4)];
                X{3}=[incomepc (incomepc.^2) (incomepc.^3) LAGincomepc (LAGincomepc.^2) (LAGincomepc.^3)];
                X{4}=[incomepc (incomepc.^2) LAGincomepc (LAGincomepc.^2)];
                X{5}=[incomepc LAGincomepc];
                X{6}=[incomepc (incomepc.^2) (incomepc.^3) (incomepc.^4) (incomepc.^5) popdens LAGincomepc (LAGincomepc.^2) (LAGincomepc.^3) (LAGincomepc.^4) (LAGincomepc.^5) LAGpopdens];
                X{7}=[incomepc (incomepc.^2) (incomepc.^3) (incomepc.^4) popdens LAGincomepc (LAGincomepc.^2) (LAGincomepc.^3) (LAGincomepc.^4) LAGpopdens];
                X{8}=[incomepc (incomepc.^2) (incomepc.^3) popdens LAGincomepc (LAGincomepc.^2) (LAGincomepc.^3) LAGpopdens];
                X{9}=[incomepc (incomepc.^2) popdens LAGincomepc (LAGincomepc.^2) LAGpopdens];
                X{10}=[incomepc popdens LAGincomepc LAGpopdens];
                X{11}=[popdens LAGpopdens];
                X{12}=[incomepc (incomepc.^2) (incomepc.^3) (incomepc.^4) (incomepc.^5) popdens (popdens.^2) LAGincomepc (LAGincomepc.^2) (LAGincomepc.^3) (LAGincomepc.^4) (LAGincomepc.^5) LAGpopdens (LAGpopdens.^2)];
                X{13}=[incomepc (incomepc.^2) (incomepc.^3) (incomepc.^4) popdens (popdens.^2) LAGincomepc (LAGincomepc.^2) (LAGincomepc.^3) (LAGincomepc.^4) LAGpopdens (LAGpopdens.^2)];
                X{14}=[incomepc (incomepc.^2) (incomepc.^3) popdens (popdens.^2) LAGincomepc (LAGincomepc.^2) (LAGincomepc.^3) LAGpopdens (LAGpopdens.^2)];
                X{15}=[incomepc (incomepc.^2) popdens (popdens.^2) LAGincomepc (LAGincomepc.^2) LAGpopdens (LAGpopdens.^2)];
                X{16}=[incomepc popdens (popdens.^2) LAGincomepc LAGpopdens (LAGpopdens.^2)];
                X{17}=[popdens (popdens.^2) LAGpopdens (LAGpopdens.^2)];
                
               
                Z{1}=[incomepcforecast80 (incomepcforecast80.^2) (incomepcforecast80.^3) (incomepcforecast80.^4) (incomepcforecast80.^5) LAGincomepcforecast80 (LAGincomepcforecast80.^2) (LAGincomepcforecast80.^3) (LAGincomepcforecast80.^4) (LAGincomepcforecast80.^5)];
                Z{2}=[incomepcforecast80 (incomepcforecast80.^2) (incomepcforecast80.^3) (incomepcforecast80.^4) LAGincomepcforecast80 (LAGincomepcforecast80.^2) (LAGincomepcforecast80.^3) (LAGincomepcforecast80.^4)];
                Z{3}=[incomepcforecast80 (incomepcforecast80.^2) (incomepcforecast80.^3) LAGincomepcforecast80 (LAGincomepcforecast80.^2) (LAGincomepcforecast80.^3)];
                Z{4}=[incomepcforecast80 (incomepcforecast80.^2) LAGincomepcforecast80 (LAGincomepcforecast80.^2)];
                Z{5}=[incomepcforecast80 LAGincomepcforecast80];
                Z{6}=[incomepcforecast80 (incomepcforecast80.^2) (incomepcforecast80.^3) (incomepcforecast80.^4) (incomepcforecast80.^5) popdensforecast80 LAGincomepcforecast80 (LAGincomepcforecast80.^2) (LAGincomepcforecast80.^3) (LAGincomepcforecast80.^4) (LAGincomepcforecast80.^5) LAGpopdensforecast80];
                Z{7}=[incomepcforecast80 (incomepcforecast80.^2) (incomepcforecast80.^3) (incomepcforecast80.^4) popdensforecast80 LAGincomepcforecast80 (LAGincomepcforecast80.^2) (LAGincomepcforecast80.^3) (LAGincomepcforecast80.^4) LAGpopdensforecast80];
                Z{8}=[incomepcforecast80 (incomepcforecast80.^2) (incomepcforecast80.^3) popdensforecast80 LAGincomepcforecast80 (LAGincomepcforecast80.^2) (LAGincomepcforecast80.^3) LAGpopdensforecast80];
                Z{9}=[incomepcforecast80 (incomepcforecast80.^2) popdensforecast80 LAGincomepcforecast80 (LAGincomepcforecast80.^2) LAGpopdensforecast80];
                Z{10}=[incomepcforecast80 popdensforecast80 LAGincomepcforecast80 LAGpopdensforecast80];
                Z{11}=[popdensforecast80 LAGpopdensforecast80];
                Z{12}=[incomepcforecast80 (incomepcforecast80.^2) (incomepcforecast80.^3) (incomepcforecast80.^4) (incomepcforecast80.^5) popdensforecast80 (popdensforecast80.^2) LAGincomepcforecast80 (LAGincomepcforecast80.^2) (LAGincomepcforecast80.^3) (LAGincomepcforecast80.^4) (LAGincomepcforecast80.^5) LAGpopdensforecast80 (LAGpopdensforecast80.^2)];
                Z{13}=[incomepcforecast80 (incomepcforecast80.^2) (incomepcforecast80.^3) (incomepcforecast80.^4) popdensforecast80 (popdensforecast80.^2) LAGincomepcforecast80 (LAGincomepcforecast80.^2) (LAGincomepcforecast80.^3) (LAGincomepcforecast80.^4) LAGpopdensforecast80 (LAGpopdensforecast80.^2)];
                Z{14}=[incomepcforecast80 (incomepcforecast80.^2) (incomepcforecast80.^3) popdensforecast80 (popdensforecast80.^2) LAGincomepcforecast80 (LAGincomepcforecast80.^2) (LAGincomepcforecast80.^3) LAGpopdensforecast80 (LAGpopdensforecast80.^2)];
                Z{15}=[incomepcforecast80 (incomepcforecast80.^2) popdensforecast80 (popdensforecast80.^2) LAGincomepcforecast80 (LAGincomepcforecast80.^2) LAGpopdensforecast80 (LAGpopdensforecast80.^2)];
                Z{16}=[incomepcforecast80 popdensforecast80 (popdensforecast80.^2) LAGincomepcforecast80 LAGpopdensforecast80 (LAGpopdensforecast80.^2)];
                Z{17}=[popdensforecast80 (popdensforecast80.^2) LAGpopdensforecast80 (LAGpopdensforecast80.^2)];
                
            % here the once for the forceasting to the year 2030  
            for c=Cc:CC
                Y{1,c}=[incomepcF{c} (incomepcF{c}.^2) (incomepcF{c}.^3) (incomepcF{c}.^4) (incomepcF{c}.^5) LAGincomepcF{c} (LAGincomepcF{c}.^2) (LAGincomepcF{c}.^3) (LAGincomepcF{c}.^4) (LAGincomepcF{c}.^5)];
                Y{2,c}=[incomepcF{c} (incomepcF{c}.^2) (incomepcF{c}.^3) (incomepcF{c}.^4) LAGincomepcF{c} (LAGincomepcF{c}.^2) (LAGincomepcF{c}.^3) (LAGincomepcF{c}.^4)];
                Y{3,c}=[incomepcF{c} (incomepcF{c}.^2) (incomepcF{c}.^3) LAGincomepcF{c} (LAGincomepcF{c}.^2) (LAGincomepcF{c}.^3)];
                Y{4,c}=[incomepcF{c} (incomepcF{c}.^2) LAGincomepcF{c} (LAGincomepcF{c}.^2)];
                Y{5,c}=[incomepcF{c} LAGincomepcF{c}];
                Y{6,c}=[incomepcF{c} (incomepcF{c}.^2) (incomepcF{c}.^3) (incomepcF{c}.^4) (incomepcF{c}.^5) popdensF LAGincomepcF{c} (LAGincomepcF{c}.^2) (LAGincomepcF{c}.^3) (LAGincomepcF{c}.^4) (LAGincomepcF{c}.^5) LAGpopdensF];
                Y{7,c}=[incomepcF{c} (incomepcF{c}.^2) (incomepcF{c}.^3) (incomepcF{c}.^4) popdensF LAGincomepcF{c} (LAGincomepcF{c}.^2) (LAGincomepcF{c}.^3) (LAGincomepcF{c}.^4) LAGpopdensF];
                Y{8,c}=[incomepcF{c} (incomepcF{c}.^2) (incomepcF{c}.^3) popdensF LAGincomepcF{c} (LAGincomepcF{c}.^2) (LAGincomepcF{c}.^3) LAGpopdensF];
                Y{9,c}=[incomepcF{c} (incomepcF{c}.^2) popdensF LAGincomepcF{c} (LAGincomepcF{c}.^2) LAGpopdensF];
                Y{10,c}=[incomepcF{c} popdensF LAGincomepcF{c} LAGpopdensF];
                Y{11,c}=[popdensF LAGpopdensF];
                Y{12,c}=[incomepcF{c} (incomepcF{c}.^2) (incomepcF{c}.^3) (incomepcF{c}.^4) (incomepcF{c}.^5) popdensF (popdensF.^2) LAGincomepcF{c} (LAGincomepcF{c}.^2) (LAGincomepcF{c}.^3) (LAGincomepcF{c}.^4) (LAGincomepcF{c}.^5) LAGpopdensF (LAGpopdensF.^2)];
                Y{13,c}=[incomepcF{c} (incomepcF{c}.^2) (incomepcF{c}.^3) (incomepcF{c}.^4) popdensF (popdensF.^2) LAGincomepcF{c} (LAGincomepcF{c}.^2) (LAGincomepcF{c}.^3) (LAGincomepcF{c}.^4) LAGpopdensF (LAGpopdensF.^2)];
                Y{14,c}=[incomepcF{c} (incomepcF{c}.^2) (incomepcF{c}.^3) popdensF (popdensF.^2) LAGincomepcF{c} (LAGincomepcF{c}.^2) (LAGincomepcF{c}.^3) LAGpopdensF (LAGpopdensF.^2)];
                Y{15,c}=[incomepcF{c} (incomepcF{c}.^2) popdensF (popdensF.^2) LAGincomepcF{c} (LAGincomepcF{c}.^2) LAGpopdensF (LAGpopdensF.^2)];
                Y{16,c}=[incomepcF{c} popdensF (popdensF.^2) LAGincomepcF{c} LAGpopdensF (LAGpopdensF.^2)];
                Y{17,c}=[popdensF (popdensF.^2) LAGpopdensF (LAGpopdensF.^2)];
            end 
           
            
            XA{1}=[incomepcA (incomepcA.^2) (incomepcA.^3) (incomepcA.^4) (incomepcA.^5) LAGincomepcA (LAGincomepcA.^2) (LAGincomepcA.^3) (LAGincomepcA.^4) (LAGincomepcA.^5)];
                XA{2}=[incomepcA (incomepcA.^2) (incomepcA.^3) (incomepcA.^4) LAGincomepcA (LAGincomepcA.^2) (LAGincomepcA.^3) (LAGincomepcA.^4)];
                XA{3}=[incomepcA (incomepcA.^2) (incomepcA.^3) LAGincomepcA (LAGincomepcA.^2) (LAGincomepcA.^3)];
                XA{4}=[incomepcA (incomepcA.^2) LAGincomepcA (LAGincomepcA.^2)];
                XA{5}=[incomepcA LAGincomepcA];
                XA{6}=[incomepcA (incomepcA.^2) (incomepcA.^3) (incomepcA.^4) (incomepcA.^5) popdensA LAGincomepcA (LAGincomepcA.^2) (LAGincomepcA.^3) (LAGincomepcA.^4) (LAGincomepcA.^5) LAGpopdensA];
                XA{7}=[incomepcA (incomepcA.^2) (incomepcA.^3) (incomepcA.^4) popdensA LAGincomepcA (LAGincomepcA.^2) (LAGincomepcA.^3) (LAGincomepcA.^4) LAGpopdensA];
                XA{8}=[incomepcA (incomepcA.^2) (incomepcA.^3) popdensA LAGincomepcA (LAGincomepcA.^2) (LAGincomepcA.^3) LAGpopdensA];
                XA{9}=[incomepcA (incomepcA.^2) popdensA LAGincomepcA (LAGincomepcA.^2) LAGpopdensA];
                XA{10}=[incomepcA popdensA LAGincomepcA LAGpopdensA];
                XA{11}=[popdensA LAGpopdensA];
                XA{12}=[incomepcA (incomepcA.^2) (incomepcA.^3) (incomepcA.^4) (incomepcA.^5) popdensA (popdensA.^2) LAGincomepcA (LAGincomepcA.^2) (LAGincomepcA.^3) (LAGincomepcA.^4) (LAGincomepcA.^5) LAGpopdensA (LAGpopdensA.^2)];
                XA{13}=[incomepcA (incomepcA.^2) (incomepcA.^3) (incomepcA.^4) popdensA (popdensA.^2) LAGincomepcA (LAGincomepcA.^2) (LAGincomepcA.^3) (LAGincomepcA.^4) LAGpopdensA (LAGpopdensA.^2)];
                XA{14}=[incomepcA (incomepcA.^2) (incomepcA.^3) popdensA (popdensA.^2) LAGincomepcA (LAGincomepcA.^2) (LAGincomepcA.^3) LAGpopdensA (LAGpopdensA.^2)];
                XA{15}=[incomepcA (incomepcA.^2) popdensA (popdensA.^2) LAGincomepcA (LAGincomepcA.^2) LAGpopdensA (LAGpopdensA.^2)];
                XA{16}=[incomepcA popdensA (popdensA.^2) LAGincomepcA LAGpopdensA (LAGpopdensA.^2)];
                XA{17}=[popdensA (popdensA.^2) LAGpopdensA (LAGpopdensA.^2)];
                
               
                ZA{1}=[incomepcforecast80A (incomepcforecast80A.^2) (incomepcforecast80A.^3) (incomepcforecast80A.^4) (incomepcforecast80A.^5) LAGincomepcforecast80A (LAGincomepcforecast80A.^2) (LAGincomepcforecast80A.^3) (LAGincomepcforecast80A.^4) (LAGincomepcforecast80A.^5)];
                ZA{2}=[incomepcforecast80A (incomepcforecast80A.^2) (incomepcforecast80A.^3) (incomepcforecast80A.^4) LAGincomepcforecast80A (LAGincomepcforecast80A.^2) (LAGincomepcforecast80A.^3) (LAGincomepcforecast80A.^4)];
                ZA{3}=[incomepcforecast80A (incomepcforecast80A.^2) (incomepcforecast80A.^3) LAGincomepcforecast80A (LAGincomepcforecast80A.^2) (LAGincomepcforecast80A.^3)];
                ZA{4}=[incomepcforecast80A (incomepcforecast80A.^2) LAGincomepcforecast80A (LAGincomepcforecast80A.^2)];
                ZA{5}=[incomepcforecast80A LAGincomepcforecast80A];
                ZA{6}=[incomepcforecast80A (incomepcforecast80A.^2) (incomepcforecast80A.^3) (incomepcforecast80A.^4) (incomepcforecast80A.^5) popdensforecast80A LAGincomepcforecast80A (LAGincomepcforecast80A.^2) (LAGincomepcforecast80A.^3) (LAGincomepcforecast80A.^4) (LAGincomepcforecast80A.^5) LAGpopdensforecast80A];
                ZA{7}=[incomepcforecast80A (incomepcforecast80A.^2) (incomepcforecast80A.^3) (incomepcforecast80A.^4) popdensforecast80A LAGincomepcforecast80A (LAGincomepcforecast80A.^2) (LAGincomepcforecast80A.^3) (LAGincomepcforecast80A.^4) LAGpopdensforecast80A];
                ZA{8}=[incomepcforecast80A (incomepcforecast80A.^2) (incomepcforecast80A.^3) popdensforecast80A LAGincomepcforecast80A (LAGincomepcforecast80A.^2) (LAGincomepcforecast80A.^3) LAGpopdensforecast80A];
                ZA{9}=[incomepcforecast80A (incomepcforecast80A.^2) popdensforecast80A LAGincomepcforecast80A (LAGincomepcforecast80A.^2) LAGpopdensforecast80A];
                ZA{10}=[incomepcforecast80A popdensforecast80A LAGincomepcforecast80A LAGpopdensforecast80A];
                ZA{11}=[popdensforecast80A LAGpopdensforecast80A];
                ZA{12}=[incomepcforecast80A (incomepcforecast80A.^2) (incomepcforecast80A.^3) (incomepcforecast80A.^4) (incomepcforecast80A.^5) popdensforecast80A (popdensforecast80A.^2) LAGincomepcforecast80A (LAGincomepcforecast80A.^2) (LAGincomepcforecast80A.^3) (LAGincomepcforecast80A.^4) (LAGincomepcforecast80A.^5) LAGpopdensforecast80A (LAGpopdensforecast80A.^2)];
                ZA{13}=[incomepcforecast80A (incomepcforecast80A.^2) (incomepcforecast80A.^3) (incomepcforecast80A.^4) popdensforecast80A (popdensforecast80A.^2) LAGincomepcforecast80A (LAGincomepcforecast80A.^2) (LAGincomepcforecast80A.^3) (LAGincomepcforecast80A.^4) LAGpopdensforecast80A (LAGpopdensforecast80A.^2)];
                ZA{14}=[incomepcforecast80A (incomepcforecast80A.^2) (incomepcforecast80A.^3) popdensforecast80A (popdensforecast80A.^2) LAGincomepcforecast80A (LAGincomepcforecast80A.^2) (LAGincomepcforecast80A.^3) LAGpopdensforecast80A (LAGpopdensforecast80A.^2)];
                ZA{15}=[incomepcforecast80A (incomepcforecast80A.^2) popdensforecast80A (popdensforecast80A.^2) LAGincomepcforecast80A (LAGincomepcforecast80A.^2) LAGpopdensforecast80A (LAGpopdensforecast80A.^2)];
                ZA{16}=[incomepcforecast80A popdensforecast80A (popdensforecast80A.^2) LAGincomepcforecast80A LAGpopdensforecast80A (LAGpopdensforecast80A.^2)];
                ZA{17}=[popdensforecast80A (popdensforecast80A.^2) LAGpopdensforecast80A (LAGpopdensforecast80A.^2)];
                
            
            
            
            end;    
            
            %number of models
            MM=size(X,2);
            ONE=(2*TN+3*QDN+TN*QDN+6+6+3*QDN); %number of regression pertubations on the fixed effect-timetrend side including sublloops with TN or QD ("ein" durchlauf)
      if l==0
                     M=size(X,2); %works this way as long as first loop is not a LHS lags one
                     Laufzeit=(L*ONE*N*M+L*ONE*N*(M-Spl))*2; %the last *2 for the new unconditional forecasts % put the number of loops (inkl. underloops) to follow the process on the screen  
               
                %w=2*L*ONE % number for waitbar
                
              %preallocate arrays to save running time (does'n later need
              %to reallocate memory)
                        b=cell(1,(Laufzeit/N));
                        f_hat=zeros(r,Laufzeit/N);
                        f_hatA=zeros(r,Laufzeit/N);
                        y_hat=cell(CC,Laufzeit/N);
    
                        bA=cell(1,(Laufzeit/N));
                        f_hatagg=zeros(r,Laufzeit/N);
                        f_hatAagg=zeros(r,Laufzeit/N);
                        y_hatA=cell(CC,Laufzeit/N);
                        
                        f_hataggA=zeros(r,Laufzeit/N);
                        f_hatAaggA=zeros(r,Laufzeit/N);
                    sb=30; %year after 1960 that should have the structural change, currently that needs to be at least 1972 2001-1972(+1 for first year) given the lag cuttoffs of the data so program can run
      end
            
% 1st loop INCLUDING State fixed effects and Time fixed effects
            for years=(Rr-sb):(Rr-sb)+TN-1; %year after 1960 that should have the structural change, changed in the current (unconditional forecast) version to 1972 through 1972+TN-1
                %waitbar(l/w)
                for m=1:MM 
                     l=l+1;
                    for r=1:N
                        Laufzeit=Laufzeit-1;        %counting index runds down 
                        clc
                        disp(['Regressions left: ',num2str(Laufzeit)]) 
                        if lag==1
                            if m<=(M-Spl) 
                            X1=[state(1:SN*(R+r),:) time(1:SN*(R+r),2:R+r) X{m}(1:SN*(R+r),:)];
                            elseif m>(M-Spl)
                            X1=[state(1:SN*(R+r),:) time(1:SN*(R+r),2:R+r) incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag}]; %allocates each model M above (at the moment 17) the right spline   
      
                            end
                        elseif lag==2
                            
                            if m<=(M-Spl) 
                            X1=[state(1:SN*(R+r),:) time(1:SN*(R+r),2:R+r) X{m}(1:SN*(R+r),:) LAGco2pc(1:SN*(R+r),:)]; %in second big loop with CO2 Lags on RHS
                            elseif m>(M-Spl)
                            X1=[state(1:SN*(R+r),:) time(1:SN*(R+r),2:R+r) incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(R+r),:)];
                            end

                            
                        end
                        [nu,ka]=size(X1);
                        [b{l},a,res]=regress(co2pc(1:SN*(R+r),:),X1);
                        
                        b_n=size(b{l},1);
                        
                        %same time trend forecast as above in the benchmark
                        x_time=[ones(R+r-1,1) linspace(1961,1961+R+r-2,R+r-1)' [zeros(years,1)' linspace(0,R+r-2-years,R+r-1-years)]'];
                        btime=regress(b{l}(SN+1:SN+R+r-1),x_time);
                        
                        y_m=(co2pc(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN));
                        
                        if lag==1
                            if m<=(M-Spl)
                            y_hat_m=[eye(SN,SN)*b{l}(1:SN)+ones(SN,1).*[(R+r-2-years+theta)*btime(3)+btime(1)+(1961+R+r-2+theta)*btime(2)]+Z{m}((SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN),:)*b{l}(SN+R+r-1+1:b_n)];
                            elseif m>(M-Spl)
                             income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(SN+R+r-1+1:b_n);
                            y_hat_m=[eye(SN,SN)*b{l}(1:SN)+ones(SN,1).*[(R+r-2-years+theta)*btime(3)+btime(1)+(1961+R+r-2+theta)*btime(2)]+X{m}((SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN),:).*income_b];
                            end
                            if m==4 && years==(Rr-sb)  && logs==2 && lag==1 && unc==1 %for 2nd benchmark
                              y_hat_B2=[eye(SN,SN)*b{l}(1:SN)+ones(SN,1).*[b{l}(SN+R+r-1)]+X{m}((SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN),:)*b{l}(SN+R+r-1+1:b_n)];  
                            end
                        elseif lag==2   %for second big loop diffrent fitted value equation since we use lagged Co2
                          if unc==1  
                            if m<=(M-Spl)
                            y_hat_m=[eye(SN,SN)*b{l}(1:SN)+ones(SN,1).*[(R+r-2-years+1)*btime(3)+btime(1)+(1961+R+r-2+1)*btime(2)]+Z{m}((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(SN+R+r-1+1:b_n-SN)+LAGco2pc((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                            for t=2:theta   %iterative calcualtion of the Co2 for theta-1 periods ahead, this is necearry for the conditional forecast in a true out-of-sample approach to get the values with such a lagged LHS variable. This is what we would have to make out-of-sample forcasts
                            y_hat_m=[eye(SN,SN)*b{l}(1:SN)+ones(SN,1).*[(R+r-2-years+t)*btime(3)+btime(1)+(1961+R+r-2+t)*btime(2)]+Z{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(SN+R+r-1+1:b_n-SN)+y_hat_m.*b{l}(b_n-SN+1:b_n)];
                            end
                            elseif m>(M-Spl)    %extra calculation for the case where we have the spline function, needs extra step..
                                income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(SN+R+r-1+1:b_n-SN);
                            y_hat_m=[eye(SN,SN)*b{l}(1:SN)+ones(SN,1).*[(R+r-2-years+1)*btime(3)+btime(1)+(1961+R+r-2+1)*btime(2)]+X{m}((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:).*income_b+LAGco2pc((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                            for t=2:theta   
                            y_hat_m=[eye(SN,SN)*b{l}(1:SN)+ones(SN,1).*[(R+r-2-years+t)*btime(3)+btime(1)+(1961+R+r-2+t)*btime(2)]+X{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:).*income_b+y_hat_m.*b{l}(b_n-SN+1:b_n)];
                            end
                            end
                          elseif unc==2
                            if m<=(M-Spl)
                            t=theta;  % in the unconditional loop we don't need to iterate to get the predicted value.
                            y_hat_m=[eye(SN,SN)*b{l}(1:SN)+ones(SN,1).*[(R+r-2-years+t)*btime(3)+btime(1)+(1961+R+r-2+t)*btime(2)]+Z{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(SN+R+r-1+1:b_n-SN)+LAGco2pc((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                            
                            elseif m>(M-Spl)    
                                income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(SN+R+r-1+1:b_n-SN);
                            t=theta;   
                            y_hat_m=[eye(SN,SN)*b{l}(1:SN)+ones(SN,1).*[(R+r-2-years+t)*btime(3)+btime(1)+(1961+R+r-2+t)*btime(2)]+X{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:).*income_b+LAGco2pc((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                            
                            end
                          
                          
                          end
                         end
                        
        %----------------------------------forecasts for each model from Rr+1 bis YEARS+YEARSFOR
        if  r==N

            if lag==1
                if m<=(M-Spl)
                    X1=[state(1:SN*(Rr),:) time(1:SN*(Rr),2:Rr) X{m}(1:SN*(Rr),:)];
                elseif m>(M-Spl)
                    X1=[state(1:SN*(Rr),:) time(1:SN*(Rr),2:Rr) incomespline_{N+1,(m-(M-Spl)+(10-Spl)),logs,lag}]; 
                end
            elseif lag==2
               
                if m<=(M-Spl)
                    X1=[state(1:SN*(Rr),:) time(1:SN*(Rr),2:Rr) X{m}(1:SN*(Rr),:) LAGco2pc(1:SN*(Rr),:)]; %in second big loop with CO2 Lags on RHS
                elseif m>(M-Spl)
                    X1=[state(1:SN*(Rr),:) time(1:SN*(Rr),2:Rr) incomespline_{N+1,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(Rr),:)];
                end

            end
clear e n k
            [b{l},a,e,int,stat]=regress(co2pc(1:SN*(Rr),:),X1);
            [n,k]=size(X1);
            %SIC
            SIC(1,l)=[log((e'*e)/(n))+((k/n)*log(n))];
            %AIC
            AIC(1,l)=[log((e'*e)/(n))+(2*k/n)];
            %simple R2
            R_2(1,l)=stat(1,1);
            % adjusted R2
            R2adj(1,l)=1-((n-1)/(n-k))*(1-R_2(1,l));
            
            b_n=size(b{l},1);

            %same time trend forecast as above in the benchmark
            x_time=[ones(Rr-1,1) linspace(1961,1961+Rr-2,Rr-1)' [zeros(years,1)' linspace(0,Rr-2-years,Rr-1-years)]'];
            btime=regress(b{l}(SN+1:SN+Rr-1),x_time);
            for c=Cc:CC
                for YF=1:YEARSFOR;
                    if lag==1
                        if m<=(M-Spl)
                            y_hat{c,l}(:,YF)=[b{l}(1:SN)+ones(SN,1).*[(Rr-2-years+YF)*btime(3)+btime(1)+(1961+Rr-2+YF)*btime(2)]+Y{m,c}((SN*(YF-1)+1:SN*(YF)),:)*b{l}(SN+Rr-1+1:b_n)];
                        elseif m>(M-Spl)
                            income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(SN+Rr-1+1:b_n);
                            y_hat{c,l}(:,YF)=[b{l}(1:SN)+ones(SN,1).*[(Rr-2-years+YF)*btime(3)+btime(1)+(1961+Rr-2+YF)*btime(2)]+Y{m,c}((SN*(YF-1)+1:SN*(YF)),:).*income_b];
                        end
                        if m==4 && years==(Rr-sb)  && logs==2 && lag==1 && unc==1 %for 2nd benchmark
                            y_hatBench2{c}(:,YF)=[b{l}(1:SN)+ones(SN,1).*[b{l}(SN+Rr-1)]+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(SN+Rr-1+1:b_n)];
                        end
                    elseif lag==2   %for second big loop diffrent fitted value equation since we use lagged Co2
                        if unc==1
                        if m<=(M-Spl)
                            if YF==1
                                y_hat{c,l}(:,YF)=[b{l}(1:SN)+ones(SN,1).*[(Rr-2-YF+1)*btime(3)+btime(1)+(1961+Rr-2+1)*btime(2)]+Y{m,c}((SN*(1-1)+1:SN),:)*b{l}(SN+Rr-1+1:b_n-SN)+co2pc((SN*(Rr-1)+1:SN*(Rr-1)+SN)).*b{l}(b_n-SN+1:b_n)];
                            end
                            if YF>1 %as above we need to iterate here to get the forecast
                                y_hat{c,l}(:,YF)=[b{l}(1:SN)+ones(SN,1).*[(Rr-2-years+YF)*btime(3)+btime(1)+(1961+Rr-2+YF)*btime(2)]+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(SN+Rr-1+1:b_n-SN)+y_hat{c,l}(:,YF-1).*b{l}(b_n-SN+1:b_n)];
                            end
                        elseif m>(M-Spl)    %extra calculation for the case where we have the spline function, needs extra step..
                            income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(SN+Rr-1+1:b_n-SN);
                            if YF==1
                                y_hat{c,l}(:,YF)=[b{l}(1:SN)+ones(SN,1).*[(Rr-2-years+1)*btime(3)+btime(1)+(1961+R+r-2+1)*btime(2)]+Y{m,c}((SN*(1-1)+1:SN),:).*income_b+co2pc((SN*(Rr-1)+1:SN*(Rr-1)+SN)).*b{l}(b_n-SN+1:b_n)];
                            end
                            if YF>1
                                y_hat{c,l}(:,YF)=[b{l}(1:SN)+ones(SN,1).*[(Rr-2-years+YF)*btime(3)+btime(1)+(1961+Rr-2+YF)*btime(2)]+Y{m,c}((SN*(YF-1)+1:SN*(YF)),:).*income_b+y_hat{c,l}(:,YF-1).*b{l}(b_n-SN+1:b_n)];
                            end
                        end
                        elseif unc==2
                         if m<=(M-Spl)
                            if YF>=1  
                                y_hat{c,l}(:,YF)=[b{l}(1:SN)+ones(SN,1).*[(Rr-2-years+YF)*btime(3)+btime(1)+(1961+Rr-2+YF)*btime(2)]+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(SN+Rr-1+1:b_n-SN)+co2pc((SN*(Rr-1-theta+YF)+1:SN*(Rr-1-theta+YF)+SN)).*b{l}(b_n-SN+1:b_n)];
                            end
                         elseif m>(M-Spl)    
                            income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(SN+Rr-1+1:b_n-SN);

                            if YF>=1
                                
                                y_hat{c,l}(:,YF)=[b{l}(1:SN)+ones(SN,1).*[(Rr-2-years+YF)*btime(3)+btime(1)+(1961+Rr-2+YF)*btime(2)]+Y{m,c}((SN*(YF-1)+1:SN*(YF)),:).*income_b+co2pc((SN*(Rr-1-theta+YF)+1:SN*(Rr-1-theta+YF)+SN)).*b{l}(b_n-SN+1:b_n)];
                            end
                         end
                        
                        
                        
                        end
                    end
                end
               if logs==2 || logs==4
               y_hat{c,l}=exp(y_hat{c,l}+(e'*e)/(n-k)/2);
               end
            end
        end

    %-------------------------------                    
                        
                        
                       
                        if logs==1 || logs==3 
                            f_hat(r,l)=(ones(SN,1)'*[([y_m]-y_hat_m).^2]);
                            f_hatA(r,l)=(ones(SN,1)'*[([y_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))]-y_hat_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))).^2]);
       
                          elseif logs==2 || logs==4
                            f_hat(r,l)=(ones(SN,1)'*[(exp(y_m)-exp(y_hat_m+(res'*res)/(nu-ka)/2)).^2]);
                            f_hatA(r,l)=(ones(SN,1)'*[(exp(y_m).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))-exp(y_hat_m+(res'*res)/(nu-ka)/2).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))).^2]);
                            
                               if m==4 && years==(Rr-sb)  && logs==2 && lag==1 && unc==1 %for 2nd benchmark
                               f_B2b(r,1)=(ones(SN,1)'*[(exp(y_m)-exp(y_hat_B2+(res'*res)/(nu-ka)/2)).^2]);
                               f_B2bA(r,1)=(ones(SN,1)'*[(exp(y_m).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))-exp(y_hat_B2+(res'*res)/(nu-ka)/2).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))).^2]);
                            end
                        end
                        clear X1 x_time btime y_m b_n y_hat_m res nu ka 
                       
                    end;
                    
                end;
           end;     

       
% 2th loop  INCLUDING State fixed effects NO TIME FIXED EFFECTS    
       
            %waitbar(l/w)
            for m=1:MM 
                 l=l+1;
                for r=1:N
                 
                    Laufzeit=Laufzeit-1;
                    clc
                    disp(['Regressions left: ',num2str(Laufzeit)])
                    
                    if lag==1
                        if m<=(M-Spl)
                        X1=[state(1:SN*(R+r),:) X{m}(1:SN*(R+r),:)];
                        elseif m>(M-Spl)
                            X1=[state(1:SN*(R+r),:) incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag}];
                            end
                    elseif lag==2
                         if m<=(M-Spl)
                        X1=[state(1:SN*(R+r),:) X{m}(1:SN*(R+r),:) LAGco2pc(1:SN*(R+r),:)];
                        elseif m>(M-Spl)
                         X1=[state(1:SN*(R+r),:) incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(R+r),:)];   
                        end
                    end
                    [nu,ka]=size(X1);
                        [b{l},a,res]=regress(co2pc(1:SN*(R+r),:),X1);
                             
                    b_n=size(b{l},1);
                    
                   y_m=(co2pc(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN));
                    if lag==1
                        if m<=(M-Spl)
                        y_hat_m=[eye(SN,SN)*b{l}(1:SN)+(Z{m}((SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN),:))*b{l}(SN+1:b_n)];
                        elseif m>(M-Spl)
                             income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(SN+1:b_n);
                        y_hat_m=[eye(SN,SN)*b{l}(1:SN)+(X{m}((SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN),:)).*income_b];
                         end
                    elseif lag==2
                        if unc==1
                        if m<=(M-Spl)
                        y_hat_m=[eye(SN,SN)*b{l}(1:SN)+(Z{m}((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:))*b{l}(SN+1:b_n-SN)+LAGco2pc((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                        for t=2:theta
                        y_hat_m=[eye(SN,SN)*b{l}(1:SN)+(Z{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:))*b{l}(SN+1:b_n-SN)+y_hat_m.*b{l}(b_n-SN+1:b_n)];
                        end
                        elseif m>(M-Spl)
                             income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(SN+1:b_n-SN);
                       y_hat_m=[eye(SN,SN)*b{l}(1:SN)+(X{m}((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)).*income_b+LAGco2pc((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                        for t=2:theta
                        y_hat_m=[eye(SN,SN)*b{l}(1:SN)+(X{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)).*income_b+y_hat_m.*b{l}(b_n-SN+1:b_n)];
                        end
                        end
                        elseif unc==2
                        if m<=(M-Spl)
                        t=theta;
                        y_hat_m=[eye(SN,SN)*b{l}(1:SN)+(Z{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:))*b{l}(SN+1:b_n-SN)+LAGco2pc((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                        elseif m>(M-Spl)
                             income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(SN+1:b_n-SN);
                        t=theta;
                        y_hat_m=[eye(SN,SN)*b{l}(1:SN)+(X{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)).*income_b+LAGco2pc((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                        
                        end
                        end
                    end
        %----------------------------------forecasts for each model from Rr+1 bis YEARS+YEARSFOR           
        if  r==N
            if lag==1
                if m<=(M-Spl)
                    X1=[state(1:SN*(Rr),:) X{m}(1:SN*(Rr),:)];
                elseif m>(M-Spl)
                    X1=[state(1:SN*(Rr),:) incomespline_{N+1,(m-(M-Spl)+(10-Spl)),logs,lag}];
                end
            elseif lag==2
                if m<=(M-Spl)
                    X1=[state(1:SN*(Rr),:) X{m}(1:SN*(Rr),:) LAGco2pc(1:SN*(Rr),:)];
                elseif m>(M-Spl)
                    X1=[state(1:SN*(Rr),:) incomespline_{N+1,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(Rr),:)];
                end
            end
            clear e n k
            [b{l},a,e,int,stat]=regress(co2pc(1:SN*(Rr),:),X1);
            [n,k]=size(X1);
            %SIC
            SIC(1,l)=[log((e'*e)/(n))+((k/n)*log(n))];
            %AIC
            AIC(1,l)=[log((e'*e)/(n))+(2*k/n)];
            %simple R2
            R_2(1,l)=stat(1,1);
            % adjusted R2
            R2adj(1,l)=1-((n-1)/(n-k))*(1-R_2(1,l));
            b_n=size(b{l},1);

            for c=Cc:CC
                for YF=1:YEARSFOR;
                    if lag==1
                        if m<=(M-Spl)
                            y_hat{c,l}(:,YF)=[b{l}(1:SN)+(Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:))*b{l}(SN+1:b_n)];
                        elseif m>(M-Spl)
                            income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(SN+1:b_n);
                            y_hat{c,l}(:,YF)=[b{l}(1:SN)+(Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)).*income_b];
                        end
                    elseif lag==2
                        if unc==1
                        if m<=(M-Spl)
                            if YF==1
                                y_hat{c,l}(:,YF)=[b{l}(1:SN)+(Y{m,c}((SN*(1-1)+1:SN*(1-1)+SN),:))*b{l}(SN+1:b_n-SN)+co2pc((SN*(Rr-1)+1:SN*(Rr-1)+SN)).*b{l}(b_n-SN+1:b_n)];
                            end
                            if YF>1
                                y_hat{c,l}(:,YF)=[b{l}(1:SN)+(Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:))*b{l}(SN+1:b_n-SN)+y_hat{c,l}(:,YF-1).*b{l}(b_n-SN+1:b_n)];
                            end
                        elseif m>(M-Spl)
                            income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(SN+1:b_n-SN);
                            if YF==1
                                y_hat{c,l}(:,YF)=[b{l}(1:SN)+(Y{m,c}((SN*(1-1)+1:SN*(1-1)+SN),:)).*income_b+co2pc((SN*(Rr-1)+1:SN*(Rr-1)+SN)).*b{l}(b_n-SN+1:b_n)];
                            end
                            if YF>1
                                y_hat{c,l}(:,YF)=[b{l}(1:SN)+(Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)).*income_b+y_hat{c,l}(:,YF-1).*b{l}(b_n-SN+1:b_n)];
                            end
                        end
                        elseif unc==2
                                                if m<=(M-Spl)
                           
                                y_hat{c,l}(:,YF)=[b{l}(1:SN)+(Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:))*b{l}(SN+1:b_n-SN)+co2pc((SN*(Rr-1-theta+YF)+1:SN*(Rr-1-theta+YF)+SN)).*b{l}(b_n-SN+1:b_n)];
                            
                        elseif m>(M-Spl)
                            income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(SN+1:b_n-SN);
                           
                            
                                y_hat{c,l}(:,YF)=[b{l}(1:SN)+(Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)).*income_b+co2pc((SN*(Rr-1-theta+YF)+1:SN*(Rr-1-theta+YF)+SN)).*b{l}(b_n-SN+1:b_n)];
                            
                        end
                        end
                        
                    end
                 end
               if logs==2 | logs==4
               y_hat{c,l}=exp(y_hat{c,l}+(e'*e)/(n-k)/2);
               end

            end
        end
%---------------------------------

         
                    if logs==1 | logs==3
                        f_hat(r,l)=(ones(SN,1)'*[([y_m]-y_hat_m).^2]);
                        f_hatA(r,l)=(ones(SN,1)'*[([y_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))]-y_hat_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))).^2]);
                        
                    elseif logs==2  | logs==4
                        f_hat(r,l)=(ones(SN,1)'*[(exp(y_m)-exp(y_hat_m+(res'*res)/(nu-ka)/2)).^2]);
                        f_hatA(r,l)=(ones(SN,1)'*[(exp(y_m).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))-exp(y_hat_m+(res'*res)/(nu-ka)/2).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))).^2]);
                        
                    end;
                    clear X1 x_time btime y_m b_n res nu ka 
                end;
                
            end;
            
% 3th loop NO STATE FIXED EFFECTS NO TIME FIXED EFFECTS
%INCLUDING US AGG REGRESSIONS 
            for m=1:MM   
                 l=l+1;
                for r=1:N
                    
                    Laufzeit=Laufzeit-1;
                    clc
                    disp(['Regressions left: ',num2str(Laufzeit)])
                    
                    if lag==1
                        if m<=(M-Spl)
                        X1=[ones(SN*(R+r),1) X{m}(1:SN*(R+r),:)];
                                     X1A=[ones((R+r),1) XA{m}(1:(R+r),:)];
                        elseif m>(M-Spl)
                            X1=[ones(SN*(R+r),1)  incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag}];
                         end
                    elseif lag==2
                        if m<=(M-Spl)
                        X1=[ones(SN*(R+r),1) X{m}(1:SN*(R+r),:) LAGco2pc(1:SN*(R+r),:)];
                                        X1A=[ones((R+r),1) XA{m}(1:(R+r),:) LAGco2pcA(1:(R+r),:)];
                         elseif m>(M-Spl)
                           X1=[ones(SN*(R+r),1) incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(R+r),:)];  
                         end
                    end
                    [nu,ka]=size(X1);
                    [b{l},a,res]=regress(co2pc(1:SN*(R+r),:),X1);
                        
                            
                    b_n=size(b{l},1);
                        
                    y_m=(co2pc(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN));
                    if m<=(M-Spl)
                                    [nuA,kaA]=size(X1A);
                                    [bA{l},aA,resA]=regress(co2pcA(1:(R+r),:),X1A);
                                    b_nA=size(bA{l},1);

                                    y_mA=(co2pcA((R+r+theta-1)+1:(R+r+theta-1)+1));
                    end;
                    if lag==1
                        if m<=(M-Spl)
                        y_hat_m=[ones(SN,1).*b{l}(1)+(Z{m}((SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN),:))*b{l}(1+1:b_n)];
                                    y_hat_mA=[ones(1,1).*bA{l}(1)+(ZA{m}((1*(R+r+theta-1)+1:1*(R+r+theta-1)+1),:))*bA{l}(1+1:b_nA)];
                        elseif m>(M-Spl)
                        income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(1+1:b_n);
                        y_hat_m=[ones(SN,1).*b{l}(1)+(X{m}((SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN),:)).*income_b];
                         end
                    elseif lag==2
                        if unc==1
                        if m<=(M-Spl)
                        y_hat_m=[ones(SN,1).*b{l}(1)+(Z{m}((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:))*b{l}(1+1:b_n-SN)+LAGco2pc((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                        for t=2:theta
                        y_hat_m=[ones(SN,1).*b{l}(1)+(Z{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:))*b{l}(1+1:b_n-SN)+y_hat_m.*b{l}(b_n-SN+1:b_n)];
                        end
                                y_hat_mA=[ones(1,1).*bA{l}(1)+(ZA{m}((1*(R+r+1-1)+1:1*(R+r+1-1)+1),:))*bA{l}(1+1:b_nA-1)+LAGco2pcA((1*(R+r+1-1)+1:1*(R+r+1-1)+1),:)*bA{l}(b_nA-1+1:b_nA)];
                                for t=2:theta
                                y_hat_mA=[ones(1,1).*bA{l}(1)+(ZA{m}((1*(R+r+t-1)+1:1*(R+r+t-1)+1),:))*bA{l}(1+1:b_nA-1)+y_hat_mA.*bA{l}(b_nA-1+1:b_nA)];
                                end
                         elseif m>(M-Spl)
                             income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(1+1:b_n-SN);
                        y_hat_m=[ones(SN,1).*b{l}(1)+(X{m}((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)).*income_b+LAGco2pc((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                        for t=2:theta
                        y_hat_m=[ones(SN,1).*b{l}(1)+(X{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)).*income_b+y_hat_m.*b{l}(b_n-SN+1:b_n)];
                        end 
                        end
                        elseif unc==2
                            if m<=(M-Spl)
                        t=theta;
                        y_hat_m=[ones(SN,1).*b{l}(1)+(Z{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:))*b{l}(1+1:b_n-SN)+LAGco2pc((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                        
                        t=theta;
                           y_hat_mA=[ones(1,1).*bA{l}(1)+(ZA{m}((1*(R+r+t-1)+1:1*(R+r+t-1)+1),:))*bA{l}(1+1:b_nA-1)+LAGco2pcA((1*(R+r+t-1)+1:1*(R+r+t-1)+1),:)*bA{l}(b_nA-1+1:b_nA)];
                         elseif m>(M-Spl)
                             income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(1+1:b_n-SN);
                        t=theta;
                        y_hat_m=[ones(SN,1).*b{l}(1)+(X{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)).*income_b+LAGco2pc((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                        
                        end
                        end
                    end
%----------------------------------forecasts for each model from Rr+1 bis YEARS+YEARSFOR           
if  r==N
    if lag==1
        if m<=(M-Spl)
            X1=[ones(SN*(Rr),1) X{m}(1:SN*(Rr),:)];
        elseif m>(M-Spl)
            X1=[ones(SN*(Rr),1)  incomespline_{N+1,(m-(M-Spl)+(10-Spl)),logs,lag}];
        end
    elseif lag==2
        if m<=(M-Spl)
            X1=[ones(SN*(Rr),1) X{m}(1:SN*(Rr),:) LAGco2pc(1:SN*(Rr),:)];
        elseif m>(M-Spl)
            X1=[ones(SN*(Rr),1) incomespline_{N+1,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(Rr),:)];
        end
    end
clear e n k
            [b{l},a,e,int,stat]=regress(co2pc(1:SN*(Rr),:),X1);
            [n,k]=size(X1);
            %SIC
            SIC(1,l)=[log((e'*e)/(n))+((k/n)*log(n))];
            %AIC
            AIC(1,l)=[log((e'*e)/(n))+(2*k/n)];
            %simple R2
            R_2(1,l)=stat(1,1);
            % adjusted R2
            R2adj(1,l)=1-((n-1)/(n-k))*(1-R_2(1,l));
    b_n=size(b{l},1);

    for c=Cc:CC
        for YF=1:YEARSFOR;
            if lag==1
                if m<=(M-Spl)
                    y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+(Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:))*b{l}(1+1:b_n)];
                elseif m>(M-Spl)
                    income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(1+1:b_n);
                    y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+(Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)).*income_b];
                end
            elseif lag==2 
                if unc==1
                if m<=(M-Spl)
                    if YF==1
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+(Y{m,c}((SN*(1-1)+1:SN*(1-1)+SN),:))*b{l}(1+1:b_n-SN)+co2pc((SN*(Rr-1)+1:SN*(Rr-1)+SN)).*b{l}(b_n-SN+1:b_n)];

                    end
                    if YF>1
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+(Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:))*b{l}(1+1:b_n-SN)+y_hat{c,l}(:,YF-1).*b{l}(b_n-SN+1:b_n)];
                    end
                elseif m>(M-Spl)
                    if YF==1
                        income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(1+1:b_n-SN);
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+(Y{m,c}((SN*(1-1)+1:SN*(1-1)+SN),:)).*income_b+co2pc((SN*(Rr-1)+1:SN*(Rr-1)+SN)).*b{l}(b_n-SN+1:b_n)];
                    end
                    if YF>1
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+(Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)).*income_b+y_hat{c,l}(:,YF-1).*b{l}(b_n-SN+1:b_n)];
                    end
                end
            elseif unc==2
                            if m<=(M-Spl)
                   
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+(Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:))*b{l}(1+1:b_n-SN)+co2pc((SN*(Rr-1-theta+YF)+1:SN*(Rr-1-theta+YF)+SN)).*b{l}(b_n-SN+1:b_n)];
                    
                elseif m>(M-Spl)
                   
                        income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(1+1:b_n-SN);

                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+(Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)).*income_b+co2pc((SN*(Rr-1-theta+YF)+1:SN*(Rr-1-theta+YF)+SN)).*b{l}(b_n-SN+1:b_n)];
                    
                end
            end
            end
         end
               if logs==2 | logs==4
               y_hat{c,l}=exp(y_hat{c,l}+(e'*e)/(n-k)/2);
               end
    end
end
%---------------------------------

         
                    if logs==1 | logs==3 
                        f_hat(r,l)=(ones(SN,1)'*[([y_m]-y_hat_m).^2]);
                        f_hatA(r,l)=(ones(SN,1)'*[([y_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))]-y_hat_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))).^2]);
                       
                       if m<=(M-Spl)
                       f_hatagg(r,l)=[(((ones(SN,1)'*[y_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))])/popA(1*(R+r+theta-1)+1:1*(R+r+theta)))-((ones(SN,1)'*(y_hat_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))))/popA(1*(R+r+theta-1)+1:1*(R+r+theta)))).^2];
                        f_hatAagg(r,l)=([(ones(SN,1)'*[y_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))]-(ones(SN,1)'*(y_hat_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))))).^2]);
                        
                        f_hataggA(r,l)=([([y_mA]-y_hat_mA).^2]);
                        f_hatAaggA(r,l)=([([y_mA.*popA(1*(R+r+theta-1)+1:1*(R+r+theta))]-y_hat_mA.*popA(1*(R+r+theta-1)+1:1*(R+r+theta))).^2]);
                       end
                    elseif logs==2  | logs==4
                        f_hat(r,l)=(ones(SN,1)'*[(exp(y_m)-exp(y_hat_m+(res'*res)/(nu-ka)/2)).^2]);
                        f_hatA(r,l)=(ones(SN,1)'*[(exp(y_m).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))-exp(y_hat_m+(res'*res)/(nu-ka)/2).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))).^2]);
                        
                        if m<=(M-Spl)
                        f_hatagg(r,l)=[(((ones(SN,1)'*[exp(y_m).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))])/popA(1*(R+r+theta-1)+1:1*(R+r+theta)))-((ones(SN,1)'*(exp(y_hat_m+(res'*res)/(nu-ka)/2).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))))/popA(1*(R+r+theta-1)+1:1*(R+r+theta)))).^2];
                        f_hatAagg(r,l)=([(ones(SN,1)'*[exp(y_m).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))]-(ones(SN,1)'*(exp(y_hat_m+(res'*res)/(nu-ka)/2).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))))).^2]);
                        
                        f_hataggA(r,l)=([([exp(y_mA)]-exp(y_hat_mA+(resA'*resA)/(nuA-kaA)/2)).^2]);
                        f_hatAaggA(r,l)=([([exp(y_mA).*popA(1*(R+r+theta-1)+1:1*(R+r+theta))]-exp(y_hat_mA+(resA'*resA)/(nuA-kaA)/2).*popA(1*(R+r+theta-1)+1:1*(R+r+theta))).^2]);
                        end
                        end;
                    clear X1 x_time btime y_m b_n res nu ka y_hat_m y_hat_mA y_mA b_nA resA nuA kaA 
                    
                end;
                
            end;
            
% 4st loop INCLUDING Time fixed effects NO STATE FIXED EFFECTS  
           
            for years=(Rr-sb):(Rr-sb)+TN-1;
                %waitbar(l/w)
                for m=1:MM    
                     l=l+1;
                    for r=1:N
                        Laufzeit=Laufzeit-1;
                        clc
                        disp(['Regressions left: ',num2str(Laufzeit)])
                        
                        
                        if lag==1
                            if m<=(M-Spl)
                            X1=[ones(SN*(R+r),1) time(1:SN*(R+r),2:R+r) X{m}(1:SN*(R+r),:)];
                            elseif m>(M-Spl)
                             X1=[ones(SN*(R+r),1) time(1:SN*(R+r),2:R+r)  incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag}];
                            end
                        elseif lag==2
                             if m<=(M-Spl)
                            X1=[ones(SN*(R+r),1) time(1:SN*(R+r),2:R+r) X{m}(1:SN*(R+r),:) LAGco2pc(1:SN*(R+r),:)]; 
                            elseif m>(M-Spl)
                                X1=[ones(SN*(R+r),1) time(1:SN*(R+r),2:R+r) incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(R+r),:)];  
                            end
                        end
                        [nu,ka]=size(X1);
                        [b{l},a,res]=regress(co2pc(1:SN*(R+r),:),X1);
                        
                        
  
                        b_n=size(b{l},1);
                        %same time trend forecast as above in the benchmark
                        
                        x_time=[ones(R+r-1,1) linspace(1961,1961+R+r-2,R+r-1)' [zeros(years,1)' linspace(0,R+r-2-years,R+r-1-years)]'];
                        btime=regress(b{l}(1+1:1+R+r-1),x_time);
                        
                        y_m=(co2pc(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN));
                        if lag==1
                            if m<=(M-Spl)
                            y_hat_m=[ones(SN,1).*b{l}(1)+ones(SN,1).*[(R+r-2-years+theta)*btime(3)+btime(1)+(1961+R+r-2+theta)*btime(2)]+Z{m}((SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN),:)*b{l}(1+R+r-1+1:b_n)];
                            elseif m>(M-Spl)
                             income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(1+R+r-1+1:b_n);
                            y_hat_m=[ones(SN,1).*b{l}(1)+ones(SN,1).*[(R+r-2-years+theta)*btime(3)+btime(1)+(1961+R+r-2+theta)*btime(2)]+X{m}((SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN),:).*income_b];
                            end
                        elseif lag==2
                            if unc==1
                            if m<=(M-Spl)
                            y_hat_m=[ones(SN,1).*b{l}(1)+ones(SN,1).*[(R+r-2-years+1)*btime(3)+btime(1)+(1961+R+r-2+1)*btime(2)]+Z{m}((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(1+R+r-1+1:b_n-SN)+LAGco2pc((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                            for t=2:theta
                            y_hat_m=[ones(SN,1).*b{l}(1)+ones(SN,1).*[(R+r-2-years+t)*btime(3)+btime(1)+(1961+R+r-2+t)*btime(2)]+Z{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(1+R+r-1+1:b_n-SN)+y_hat_m.*b{l}(b_n-SN+1:b_n)];
                            end
                              elseif m>(M-Spl)
                             income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(1+R+r-1+1:b_n-SN);
                             y_hat_m=[ones(SN,1).*b{l}(1)+ones(SN,1).*[(R+r-2-years+1)*btime(3)+btime(1)+(1961+R+r-2+1)*btime(2)]+X{m}((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:).*income_b+LAGco2pc((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                            for t=2:theta
                            y_hat_m=[ones(SN,1).*b{l}(1)+ones(SN,1).*[(R+r-2-years+t)*btime(3)+btime(1)+(1961+R+r-2+t)*btime(2)]+X{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:).*income_b+y_hat_m.*b{l}(b_n-SN+1:b_n)];
                            end
                            end
                            elseif unc==2
                                                        if m<=(M-Spl)
                            t=theta;
                            y_hat_m=[ones(SN,1).*b{l}(1)+ones(SN,1).*[(R+r-2-years+t)*btime(3)+btime(1)+(1961+R+r-2+t)*btime(2)]+Z{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(1+R+r-1+1:b_n-SN)+LAGco2pc((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                            elseif m>(M-Spl)
                            income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(1+R+r-1+1:b_n-SN);
                            t=theta;
                            y_hat_m=[ones(SN,1).*b{l}(1)+ones(SN,1).*[(R+r-2-years+t)*btime(3)+btime(1)+(1961+R+r-2+t)*btime(2)]+X{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:).*income_b+LAGco2pc((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                            
                            end
                            end
                            
                        end
%----------------------------------forecasts for each model from Rr+1 bis YEARS+YEARSFOR           
if  r==N
    if lag==1
        if m<=(M-Spl)
            X1=[ones(SN*(Rr),1) time(1:SN*(Rr),2:Rr) X{m}(1:SN*(Rr),:)];
        elseif m>(M-Spl)
            X1=[ones(SN*(Rr),1) time(1:SN*(Rr),2:Rr)  incomespline_{N+1,(m-(M-Spl)+(10-Spl)),logs,lag}];
        end
    elseif lag==2
        if m<=(M-Spl)
            X1=[ones(SN*(Rr),1) time(1:SN*(Rr),2:Rr) X{m}(1:SN*(Rr),:) LAGco2pc(1:SN*(Rr),:)];
        elseif m>(M-Spl)
            X1=[ones(SN*(Rr),1) time(1:SN*(Rr),2:Rr) incomespline_{N+1,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(Rr),:)];
        end
    end
clear e n k

            [b{l},a,e,int,stat]=regress(co2pc(1:SN*(Rr),:),X1);
            [n,k]=size(X1);
            %SIC
            SIC(1,l)=[log((e'*e)/(n))+((k/n)*log(n))];
            %AIC
            AIC(1,l)=[log((e'*e)/(n))+(2*k/n)];
            %simple R2
            R_2(1,l)=stat(1,1);
            % adjusted R2
            R2adj(1,l)=1-((n-1)/(n-k))*(1-R_2(1,l));

    b_n=size(b{l},1);
    %same time trend forecast as above in the benchmark

    x_time=[ones(Rr-1,1) linspace(1961,1961+Rr-2,Rr-1)' [zeros(years,1)' linspace(0,Rr-2-years,Rr-1-years)]'];
    btime=regress(b{l}(1+1:1+Rr-1),x_time);

    for c=Cc:CC
        for YF=1:YEARSFOR;
            if lag==1
                if m<=(M-Spl)
                    y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+ones(SN,1).*[(Rr-2-years+YF)*btime(3)+btime(1)+(1961+Rr-2+YF)*btime(2)]+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(1+Rr-1+1:b_n)];
                elseif m>(M-Spl)
                    income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(1+Rr-1+1:b_n);
                    y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+ones(SN,1).*[(Rr-2-years+YF)*btime(3)+btime(1)+(1961+Rr-2+YF)*btime(2)]+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b];
                end
            elseif lag==2 
               if unc==1
                if m<=(M-Spl)
                    if YF==1
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+ones(SN,1).*[(Rr-2-years+1)*btime(3)+btime(1)+(1961+Rr-2+1)*btime(2)]+Y{m,c}((SN*(1-1)+1:SN*(1-1)+SN),:)*b{l}(1+Rr-1+1:b_n-SN)+co2pc((SN*(Rr-1)+1:SN*(Rr-1)+SN)).*b{l}(b_n-SN+1:b_n)];

                    end
                    if YF>1
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+ones(SN,1).*[(Rr-2-years+YF)*btime(3)+btime(1)+(1961+Rr-2+YF)*btime(2)]+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(1+Rr-1+1:b_n-SN)+y_hat{c,l}(:,YF-1).*b{l}(b_n-SN+1:b_n)];
                    end
                elseif m>(M-Spl)
                    if YF==1
                        income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(1+Rr-1+1:b_n-SN);
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+ones(SN,1).*[(Rr-2-years+1)*btime(3)+btime(1)+(1961+Rr-2+1)*btime(2)]+Y{m,c}((SN*(1-1)+1:SN*(1-1)+SN),:).*income_b+co2pc((SN*(Rr-1)+1:SN*(Rr-1)+SN)).*b{l}(b_n-SN+1:b_n)];
                    end
                    if YF>1
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+ones(SN,1).*[(Rr-2-years+YF)*btime(3)+btime(1)+(1961+Rr-2+YF)*btime(2)]+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b+y_hat{c,l}(:,YF-1).*b{l}(b_n-SN+1:b_n)];
                    end
                end
               elseif unc==2
                               if m<=(M-Spl)
                    
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+ones(SN,1).*[(Rr-2-years+YF)*btime(3)+btime(1)+(1961+Rr-2+YF)*btime(2)]+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(1+Rr-1+1:b_n-SN)+co2pc((SN*(Rr-1-theta+YF)+1:SN*(Rr-1-theta+YF)+SN)).*b{l}(b_n-SN+1:b_n)];
                    
                elseif m>(M-Spl)
                    
                        income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(1+Rr-1+1:b_n-SN);
                    
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+ones(SN,1).*[(Rr-2-years+YF)*btime(3)+btime(1)+(1961+Rr-2+YF)*btime(2)]+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b+co2pc((SN*(Rr-1-theta+YF)+1:SN*(Rr-1-theta+YF)+SN)).*b{l}(b_n-SN+1:b_n)];
                    
                end
                end
               
            end
         end
               if logs==2 | logs==4
               y_hat{c,l}=exp(y_hat{c,l}+(e'*e)/(n-k)/2);
               end
    end
end
%---------------------------------
                       
                        if logs==1 | logs==3 
                            f_hat(r,l)=(ones(SN,1)'*[([y_m]-y_hat_m).^2]);
                            f_hatA(r,l)=(ones(SN,1)'*[([y_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))]-y_hat_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))).^2]);
                            
                        elseif logs==2  | logs==4
                            f_hat(r,l)=(ones(SN,1)'*[(exp(y_m)-exp(y_hat_m+(res'*res)/(nu-ka)/2)).^2]);
                            f_hatA(r,l)=(ones(SN,1)'*[(exp(y_m).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))-exp(y_hat_m+(res'*res)/(nu-ka)/2).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))).^2]);
                            
                        end;
                        clear X1 x_time btime y_m b_n  res nu ka 
                    end;
                    
                end;
                
            end;
            
          
            
% 5rd loop INCLUDING State fixed effects  and TIMETREND   
            %waitbar(l/w)
            for m=1:MM 
                 l=l+1;
                for r=1:N
                    Laufzeit=Laufzeit-1;
                    clc
                    disp(['Regressions left: ',num2str(Laufzeit)])
                    
                    
                    if lag==1
                        if m<=(M-Spl)    
                        X1=[state(1:SN*(R+r),:) timetrend(1:SN*(R+r)) X{m}(1:SN*(R+r),:)];
                        elseif m>(M-Spl)
                             X1=[state(1:SN*(R+r),:) timetrend(1:SN*(R+r))   incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag}];
                            end
                    elseif lag==2
                         if m<=(M-Spl)  
                        X1=[state(1:SN*(R+r),:) timetrend(1:SN*(R+r)) X{m}(1:SN*(R+r),:) LAGco2pc(1:SN*(R+r),:)];  
                         elseif m>(M-Spl)
                           X1=[state(1:SN*(R+r),:) timetrend(1:SN*(R+r))  incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(R+r),:)];    
                         end
                    end
                    [nu,ka]=size(X1);
                        [b{l},a,res]=regress(co2pc(1:SN*(R+r),:),X1);
                            
                    b_n=size(b{l},1);
                    
                    y_m=(co2pc(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN));
                    if lag==1
                        if m<=(M-Spl)
                        y_hat_m=[eye(SN,SN)*b{l}(1:SN)+timetrend(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN).*b{l}(SN+1)+Z{m}((SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN),:)*b{l}(SN+1+1:b_n)];
                        elseif m>(M-Spl)
                             income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(SN+1+1:b_n);
                            y_hat_m=[eye(SN,SN)*b{l}(1:SN)+timetrend(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN).*b{l}(SN+1)+X{m}((SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN),:).*income_b];
                            end
                    elseif lag==2
                        if unc==1
                        if m<=(M-Spl)
                        y_hat_m=[eye(SN,SN)*b{l}(1:SN)+timetrend(SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN).*b{l}(SN+1)+Z{m}((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(SN+1+1:b_n-SN)+LAGco2pc((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                        for t=2:theta
                        y_hat_m=[eye(SN,SN)*b{l}(1:SN)+timetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(SN+1)+Z{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(SN+1+1:b_n-SN)+y_hat_m.*b{l}(b_n-SN+1:b_n)];
                        end        
                         elseif m>(M-Spl)
                             income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(SN+1+1:b_n-SN);
                         y_hat_m=[eye(SN,SN)*b{l}(1:SN)+timetrend(SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN).*b{l}(SN+1)+X{m}((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:).*income_b+LAGco2pc((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                        for t=2:theta
                        y_hat_m=[eye(SN,SN)*b{l}(1:SN)+timetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(SN+1)+X{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:).*income_b+y_hat_m.*b{l}(b_n-SN+1:b_n)];
                        end  
                        end
                        elseif unc==2
                                                if m<=(M-Spl)
                        t=theta;
                        y_hat_m=[eye(SN,SN)*b{l}(1:SN)+timetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(SN+1)+Z{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(SN+1+1:b_n-SN)+LAGco2pc((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                                
                         elseif m>(M-Spl)
                             income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(SN+1+1:b_n-SN);
                        t=theta;
                        y_hat_m=[eye(SN,SN)*b{l}(1:SN)+timetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(SN+1)+X{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:).*income_b+LAGco2pc((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                         
                        end
                        end
                        
                    end
%----------------------------------forecasts for each model from Rr+1 bis YEARS+YEARSFOR           
if  r==N
    if lag==1
        if m<=(M-Spl)
            X1=[state(1:SN*(Rr),:) timetrend(1:SN*(Rr)) X{m}(1:SN*(Rr),:)];
        elseif m>(M-Spl)
            X1=[state(1:SN*(Rr),:) timetrend(1:SN*(Rr))   incomespline_{N+1,(m-(M-Spl)+(10-Spl)),logs,lag}];
        end
    elseif lag==2
        if m<=(M-Spl)
            X1=[state(1:SN*(Rr),:) timetrend(1:SN*(Rr)) X{m}(1:SN*(Rr),:) LAGco2pc(1:SN*(Rr),:)];
        elseif m>(M-Spl)
            X1=[state(1:SN*(Rr),:) timetrend(1:SN*(Rr))  incomespline_{N+1,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(Rr),:)];
        end
    end
clear e n k
            [b{l},a,e,int,stat]=regress(co2pc(1:SN*(Rr),:),X1);
            [n,k]=size(X1);
            %SIC
            SIC(1,l)=[log((e'*e)/(n))+((k/n)*log(n))];
            %AIC
            AIC(1,l)=[log((e'*e)/(n))+(2*k/n)];
            %simple R2
            R_2(1,l)=stat(1,1);
            % adjusted R2
            R2adj(1,l)=1-((n-1)/(n-k))*(1-R_2(1,l));
    b_n=size(b{l},1);

    for c=Cc:CC
        for YF=1:YEARSFOR;
            if lag==1
                if m<=(M-Spl)
                    y_hat{c,l}(:,YF)=[b{l}(1:SN)+timetrend(SN*(YF-1)+1:SN*(YF-1)+SN).*b{l}(SN+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(SN+1+1:b_n)];
                elseif m>(M-Spl)
                    income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(SN+1+1:b_n);
                    y_hat{c,l}(:,YF)=[b{l}(1:SN)+timetrend(SN*(YF-1)+1:SN*(YF-1)+SN).*b{l}(SN+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b];
                end
            elseif lag==2 
                if unc==1
                if m<=(M-Spl)
                    if YF==1
                        y_hat{c,l}(:,YF)=[b{l}(1:SN)+timetrend(SN*(1-1)+1:SN*(1-1)+SN).*b{l}(SN+1)+Y{m,c}((SN*(1-1)+1:SN*(1-1)+SN),:)*b{l}(SN+1+1:b_n-SN)+co2pc((SN*(Rr-1)+1:SN*(Rr-1)+SN)).*b{l}(b_n-SN+1:b_n)];
                    end
                    if YF>1
                        y_hat{c,l}(:,YF)=[b{l}(1:SN)+timetrend(SN*(YF-1)+1:SN*(YF-1)+SN).*b{l}(SN+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(SN+1+1:b_n-SN)+y_hat{c,l}(:,YF-1).*b{l}(b_n-SN+1:b_n)];
                    end
                elseif m>(M-Spl)
                    if YF==1
                        income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(SN+1+1:b_n-SN);
                        y_hat{c,l}(:,YF)=[b{l}(1:SN)+timetrend(SN*(1-1)+1:SN*(1-1)+SN).*b{l}(SN+1)+Y{m,c}((SN*(1-1)+1:SN*(1-1)+SN),:).*income_b+co2pc((SN*(Rr-1)+1:SN*(Rr-1)+SN)).*b{l}(b_n-SN+1:b_n)];
                    end
                    if YF>1
                        y_hat{c,l}(:,YF)=[b{l}(1:SN)+timetrend(SN*(YF-1)+1:SN*(YF-1)+SN).*b{l}(SN+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b+y_hat{c,l}(:,YF-1).*b{l}(b_n-SN+1:b_n)];
                    end
                end
                elseif unc==2
                                if m<=(M-Spl)

                        y_hat{c,l}(:,YF)=[b{l}(1:SN)+timetrend(SN*(YF-1)+1:SN*(YF-1)+SN).*b{l}(SN+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(SN+1+1:b_n-SN)+co2pc((SN*(Rr-1-theta+YF)+1:SN*(Rr-1-theta+YF)+SN)).*b{l}(b_n-SN+1:b_n)];
                    
                elseif m>(M-Spl)
                   
                        income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(SN+1+1:b_n-SN);

                        y_hat{c,l}(:,YF)=[b{l}(1:SN)+timetrend(SN*(YF-1)+1:SN*(YF-1)+SN).*b{l}(SN+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b+co2pc((SN*(Rr-1-theta+YF)+1:SN*(Rr-1-theta+YF)+SN)).*b{l}(b_n-SN+1:b_n)];
                    
                end
                end
                
            end
         end
               if logs==2 | logs==4
               y_hat{c,l}=exp(y_hat{c,l}+(e'*e)/(n-k)/2);
               end
    end
end
%---------------------------------

                   
                    if logs==1 | logs==3 
                        f_hat(r,l)=(ones(SN,1)'*[([y_m]-y_hat_m).^2]);
                        f_hatA(r,l)=(ones(SN,1)'*[([y_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))]-y_hat_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))).^2]);
                        
                    elseif logs==2  | logs==4
                        f_hat(r,l)=(ones(SN,1)'*[(exp(y_m)-exp(y_hat_m+(res'*res)/(nu-ka)/2)).^2]);
                        f_hatA(r,l)=(ones(SN,1)'*[(exp(y_m).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))-exp(y_hat_m+(res'*res)/(nu-ka)/2).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))).^2]);
                        
                    end;
                    clear X1 x_time btime y_m b_n res nu ka 
                end;
                
            end;
            
            
% 6th loop INCLUDING TIMETREND      NO STATE FIXED EFFECTS 
%INCLUDING US AGG REGRESSIONS 
            for m=1:MM  
                 l=l+1;
                for r=1:N
                    Laufzeit=Laufzeit-1;
                    clc
                    disp(['Regressions left: ',num2str(Laufzeit)])
                    
                    
                    if lag==1
                        if m<=(M-Spl)
                        X1=[ones(SN*(R+r),1) timetrend(1:SN*(R+r)) X{m}(1:SN*(R+r),:)];
                                    X1A=[ones((R+r),1) timetrendA(1:1*(R+r)) XA{m}(1:1*(R+r),:)];
                        elseif m>(M-Spl)
                             X1=[ones(SN*(R+r),1) timetrend(1:SN*(R+r)) incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag}];
                            end
                    elseif lag==2
                        if m<=(M-Spl)
                        X1=[ones(SN*(R+r),1) timetrend(1:SN*(R+r)) X{m}(1:SN*(R+r),:) LAGco2pc(1:SN*(R+r),:)];  
                                         X1A=[ones(1*(R+r),1) timetrendA(1:1*(R+r)) XA{m}(1:1*(R+r),:) LAGco2pcA(1:1*(R+r),:)]; 
                        elseif m>(M-Spl)
                            X1=[ones(SN*(R+r),1) timetrend(1:SN*(R+r)) incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(R+r),:)];  
                        end
                    end
                    [nu,ka]=size(X1);
                        [b{l},a,res]=regress(co2pc(1:SN*(R+r),:),X1);
                        
                             
                    b_n=size(b{l},1);
                    
                    y_m=(co2pc(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN));
                    if m<=(M-Spl)
                                  [nuA,kaA]=size(X1A);
                                    [bA{l},aA,resA]=regress(co2pcA(1:1*(R+r),:),X1A);

                                      
                                b_nA=size(bA{l},1);

                                y_mA=(co2pcA(1*(R+r+theta-1)+1:1*(R+r+theta-1)+1));
                    end
                                if lag==1
                        if m<=(M-Spl)
                        y_hat_m=[ones(SN,1).*b{l}(1)+timetrend(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN).*b{l}(1+1)+Z{m}((SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN),:)*b{l}(1+1+1:b_n)];
                                         y_hat_mA=[ones(1,1).*b{l}(1)+timetrendA(1*(R+r+theta-1)+1:1*(R+r+theta-1)+1).*bA{l}(1+1)+ZA{m}((1*(R+r+theta-1)+1:1*(R+r+theta-1)+1),:)*bA{l}(1+1+1:b_nA)];
                        elseif m>(M-Spl)
                             income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(1+1+1:b_n);
                            y_hat_m=[ones(SN,1).*b{l}(1)+timetrend(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN).*b{l}(1+1)+X{m}((SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN),:).*income_b];
                            end
                    elseif lag==2
                        if unc==1
                        if m<=(M-Spl)
                        y_hat_m=[ones(SN,1).*b{l}(1)+timetrend(SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN).*b{l}(1+1)+Z{m}((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(1+1+1:b_n-SN)+LAGco2pc((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                        for t=2:theta
                        y_hat_m=[ones(SN,1).*b{l}(1)+timetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(1+1)+Z{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(1+1+1:b_n-SN)+y_hat_m.*b{l}(b_n-SN+1:b_n)];
                        end    
                        
                                    y_hat_mA=[ones(1,1).*bA{l}(1)+timetrendA(1*(R+r+1-1)+1:1*(R+r+1-1)+1).*bA{l}(1+1)+ZA{m}((1*(R+r+1-1)+1:1*(R+r+1-1)+1),:)*bA{l}(1+1+1:b_nA-1)+LAGco2pcA((1*(R+r+1-1)+1:1*(R+r+1-1)+1),:)*bA{l}(b_nA-1+1:b_nA)];
                                    for t=2:theta
                                    y_hat_mA=[ones(1,1).*bA{l}(1)+timetrendA(1*(R+r+t-1)+1:1*(R+r+t-1)+1).*bA{l}(1+1)+ZA{m}((1*(R+r+t-1)+1:1*(R+r+t-1)+1),:)*bA{l}(1+1+1:b_nA-1)+y_hat_mA.*bA{l}(b_nA-1+1:b_nA)];
                                    end  
                        elseif m>(M-Spl)
                             income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(1+1+1:b_n-SN);
                             y_hat_m=[ones(SN,1).*b{l}(1)+timetrend(SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN).*b{l}(1+1)+X{m}((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:).*income_b+LAGco2pc((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                        for t=2:theta
                        y_hat_m=[ones(SN,1).*b{l}(1)+timetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(1+1)+X{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:).*income_b+y_hat_m.*b{l}(b_n-SN+1:b_n)];
                        end  
                        end
                        elseif unc==2
                                                if m<=(M-Spl)
                        t=theta;
                        y_hat_m=[ones(SN,1).*b{l}(1)+timetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(1+1)+Z{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(1+1+1:b_n-SN)+LAGco2pc((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                            
                        
                                    t=theta;
                                    y_hat_mA=[ones(1,1).*bA{l}(1)+timetrendA(1*(R+r+t-1)+1:1*(R+r+t-1)+1).*bA{l}(1+1)+ZA{m}((1*(R+r+t-1)+1:1*(R+r+t-1)+1),:)*bA{l}(1+1+1:b_nA-1)+LAGco2pcA((1*(R+r+t-1)+1:1*(R+r+t-1)+1),:)*bA{l}(b_nA-1+1:b_nA)];
                                      
                        elseif m>(M-Spl)
                             income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(1+1+1:b_n-SN);
                        t=theta;
                        y_hat_m=[ones(SN,1).*b{l}(1)+timetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(1+1)+X{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:).*income_b+LAGco2pc((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                          
                    end
                        end
                        
                    end
%----------------------------------forecasts for each model from Rr+1 bis YEARS+YEARSFOR           
if  r==N
    if lag==1
        if m<=(M-Spl)
            X1=[ones(SN*(Rr),1) timetrend(1:SN*(Rr)) X{m}(1:SN*(Rr),:)];
        elseif m>(M-Spl)
            X1=[ones(SN*(Rr),1) timetrend(1:SN*(Rr)) incomespline_{N+1,(m-(M-Spl)+(10-Spl)),logs,lag}];
        end
    elseif lag==2
        if m<=(M-Spl)
            X1=[ones(SN*(Rr),1) timetrend(1:SN*(Rr)) X{m}(1:SN*(Rr),:) LAGco2pc(1:SN*(Rr),:)];
        elseif m>(M-Spl)
            X1=[ones(SN*(Rr),1) timetrend(1:SN*(Rr)) incomespline_{N+1,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(Rr),:)];
        end
    end
clear e n k
            [b{l},a,e,int,stat]=regress(co2pc(1:SN*(Rr),:),X1);
            [n,k]=size(X1);
            %SIC
            SIC(1,l)=[log((e'*e)/(n))+((k/n)*log(n))];
            %AIC
            AIC(1,l)=[log((e'*e)/(n))+(2*k/n)];
            %simple R2
            R_2(1,l)=stat(1,1);
            % adjusted R2
            R2adj(1,l)=1-((n-1)/(n-k))*(1-R_2(1,l));
    b_n=size(b{l},1);

    for c=Cc:CC
        for YF=1:YEARSFOR;
            if lag==1
                if m<=(M-Spl)
                    y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+timetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(1+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(1+1+1:b_n)];
                elseif m>(M-Spl)
                    income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(1+1+1:b_n);
                    y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+timetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(1+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b];
                end
            elseif lag==2 
                if unc==1
                if m<=(M-Spl)
                    if YF==1
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+timetrend(SN*(Rr+1-1)+1:SN*(Rr+1-1)+SN).*b{l}(1+1)+Y{m,c}((SN*(1-1)+1:SN*(1-1)+SN),:)*b{l}(1+1+1:b_n-SN)+co2pc((SN*(Rr-1)+1:SN*(Rr-1)+SN)).*b{l}(b_n-SN+1:b_n)];
                    end
                    if YF>1
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+timetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(1+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(1+1+1:b_n-SN)+y_hat{c,l}(:,YF-1).*b{l}(b_n-SN+1:b_n)];
                    end
                elseif m>(M-Spl)
                    if YF==1
                        income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(1+1+1:b_n-SN);
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+timetrend(SN*(Rr+1-1)+1:SN*(Rr+1-1)+SN).*b{l}(1+1)+Y{m,c}((SN*(1-1)+1:SN*(1-1)+SN),:).*income_b+co2pc((SN*(Rr-1)+1:SN*(Rr-1)+SN)).*b{l}(b_n-SN+1:b_n)];
                    end
                    if YF>1
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+timetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(1+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b+y_hat{c,l}(:,YF-1).*b{l}(b_n-SN+1:b_n)];
                    end
                end
                elseif unc==2
                if m<=(M-Spl)
              
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+timetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(1+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(1+1+1:b_n-SN)+co2pc((SN*(Rr-1-theta+YF)+1:SN*(Rr-1-theta+YF)+SN)).*b{l}(b_n-SN+1:b_n)];
                    
                elseif m>(M-Spl)
                    
                        income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(1+1+1:b_n-SN);
                    
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+timetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(1+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b+co2pc((SN*(Rr-1-theta+YF)+1:SN*(Rr-1-theta+YF)+SN)).*b{l}(b_n-SN+1:b_n)];
                    
                end
                end
                
            end
         end
               if logs==2 | logs==4
               y_hat{c,l}=exp(y_hat{c,l}+(e'*e)/(n-k)/2);
               end
    end
end
%---------------------------------

                    
                      if logs==1 | logs==3 
                        f_hat(r,l)=(ones(SN,1)'*[([y_m]-y_hat_m).^2]);
                        f_hatA(r,l)=(ones(SN,1)'*[([y_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))]-y_hat_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))).^2]);
                      
                       if m<=(M-Spl) 
                       f_hatagg(r,l)=[(((ones(SN,1)'*[y_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))])/popA(1*(R+r+theta-1)+1:1*(R+r+theta)))-((ones(SN,1)'*(y_hat_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))))/popA(1*(R+r+theta-1)+1:1*(R+r+theta)))).^2];
                        f_hatAagg(r,l)=([(ones(SN,1)'*[y_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))]-(ones(SN,1)'*(y_hat_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))))).^2]);
                        
                        f_hataggA(r,l)=([([y_mA]-y_hat_mA).^2]);
                        f_hatAaggA(r,l)=([([y_mA.*popA(1*(R+r+theta-1)+1:1*(R+r+theta))]-y_hat_mA.*popA(1*(R+r+theta-1)+1:1*(R+r+theta))).^2]);
                       end
                       elseif logs==2  | logs==4
                        f_hat(r,l)=(ones(SN,1)'*[(exp(y_m)-exp(y_hat_m+(res'*res)/(nu-ka)/2)).^2]);
                        f_hatA(r,l)=(ones(SN,1)'*[(exp(y_m).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))-exp(y_hat_m+(res'*res)/(nu-ka)/2).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))).^2]);
                        
                       if m<=(M-Spl)
                        f_hatagg(r,l)=[(((ones(SN,1)'*[exp(y_m).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))])/popA(1*(R+r+theta-1)+1:1*(R+r+theta)))-((ones(SN,1)'*(exp(y_hat_m+(res'*res)/(nu-ka)/2).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))))/popA(1*(R+r+theta-1)+1:1*(R+r+theta)))).^2];
                        f_hatAagg(r,l)=([(ones(SN,1)'*[exp(y_m).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))]-(ones(SN,1)'*(exp(y_hat_m+(res'*res)/(nu-ka)/2).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))))).^2]);
                        
                        f_hataggA(r,l)=([([exp(y_mA)]-exp(y_hat_mA+(resA'*resA)/(nuA-kaA)/2)).^2]);
                        f_hatAaggA(r,l)=([([exp(y_mA).*popA(1*(R+r+theta-1)+1:1*(R+r+theta))]-exp(y_hat_mA+(resA'*resA)/(nuA-kaA)/2).*popA(1*(R+r+theta-1)+1:1*(R+r+theta))).^2]);
                       end
                       end;
                    clear X1 x_time btime y_m b_n res nu ka y_hat_m y_hat_mA y_mA b_nA resA nuA kaA 
                end;
                
            end;
% 7th loop NO STATE FIXED EFFECTS---BUT QUALITY DUMMIES   NO TIME FIXED EFFECTS
            for q=1:QDN   
                 %waitbar(l/w)
                for m=1:MM   
                     l=l+1;
                    for r=1:N
                        
                        Laufzeit=Laufzeit-1;
                        clc
                        disp(['Regressions left: ',num2str(Laufzeit)])
                        
                        if lag==1
                            if m<=(M-Spl)
                            X1=[ones(SN*(R+r),1) QD{q}(1:SN*(R+r),:) X{m}(1:SN*(R+r),:)];
                            elseif m>(M-Spl)
                             X1=[ones(SN*(R+r),1) QD{q}(1:SN*(R+r),:) incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag}];
                            end
                        elseif lag==2
                            if m<=(M-Spl)
                            X1=[ones(SN*(R+r),1) QD{q}(1:SN*(R+r),:) X{m}(1:SN*(R+r),:) LAGco2pc(1:SN*(R+r),:)];
                            elseif m>(M-Spl)
                               X1=[ones(SN*(R+r),1) QD{q}(1:SN*(R+r),:) incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(R+r),:)];   
                            end
                        end
                        
                        [nu,ka]=size(X1);
                        [b{l},a,res]=regress(co2pc(1:SN*(R+r),:),X1);
                               
                        b_n=size(b{l},1);
                        q_k=size(QD{q},2);
                        
                        y_m=(co2pc(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN));
                        if lag==1
                            if m<=(M-Spl)
                            y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+(Z{m}((SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN),:))*b{l}(q_k+2:b_n)];
                            elseif m>(M-Spl)
                             income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(q_k+2:b_n);
                            y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+(X{m}((SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN),:)).*income_b];
                            end 
                        elseif lag==2
                            if unc==1
                            if m<=(M-Spl)
                            y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+(Z{m}((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:))*b{l}(q_k+2:b_n-SN)+LAGco2pc((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                            for t=2:theta
                            y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+(Z{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:))*b{l}(q_k+2:b_n-SN)+y_hat_m.*b{l}(b_n-SN+1:b_n)];
                            end  
                            elseif m>(M-Spl)
                             income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(q_k+2:b_n-SN);
                             y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+(X{m}((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)).*income_b+LAGco2pc((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                            for t=2:theta
                            y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+(X{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)).*income_b+y_hat_m.*b{l}(b_n-SN+1:b_n)];
                            end  
                        end
                            elseif unc==2
                                                        if m<=(M-Spl)
                            t=theta;
                            y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+(Z{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:))*b{l}(q_k+2:b_n-SN)+LAGco2pc((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                              
                            elseif m>(M-Spl)
                             income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(q_k+2:b_n-SN);
                           t=theta;
                            y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+(X{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)).*income_b+LAGco2pc((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                              
                        end
                            end
                            
                        end
%----------------------------------forecasts for each model from Rr+1 bis YEARS+YEARSFOR           
if  r==N
    if lag==1
        if m<=(M-Spl)
            X1=[ones(SN*(Rr),1) QD{q}(1:SN*(Rr),:) X{m}(1:SN*(Rr),:)];
        elseif m>(M-Spl)
            X1=[ones(SN*(Rr),1) QD{q}(1:SN*(Rr),:) incomespline_{N+1,(m-(M-Spl)+(10-Spl)),logs,lag}];
        end
    elseif lag==2
        if m<=(M-Spl)
            X1=[ones(SN*(Rr),1) QD{q}(1:SN*(Rr),:) X{m}(1:SN*(Rr),:) LAGco2pc(1:SN*(Rr),:)];
        elseif m>(M-Spl)
            X1=[ones(SN*(Rr),1) QD{q}(1:SN*(Rr),:) incomespline_{N+1,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(Rr),:)];
        end
    end

clear e n k
            [b{l},a,e,int,stat]=regress(co2pc(1:SN*(Rr),:),X1);
            [n,k]=size(X1);
            %SIC
            SIC(1,l)=[log((e'*e)/(n))+((k/n)*log(n))];
            %AIC
            AIC(1,l)=[log((e'*e)/(n))+(2*k/n)];
            %simple R2
            R_2(1,l)=stat(1,1);
            % adjusted R2
            R2adj(1,l)=1-((n-1)/(n-k))*(1-R_2(1,l));
    b_n=size(b{l},1);
    q_k=size(QD{q},2);

    for c=Cc:CC
        for YF=1:YEARSFOR;
            if lag==1
                if m<=(M-Spl)
                    y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+(Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:))*b{l}(q_k+2:b_n)];
                elseif m>(M-Spl)
                    income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(q_k+2:b_n);
                    y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+(Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)).*income_b];
                end
            elseif lag==2 
                if unc==1
                if m<=(M-Spl)
                    if YF==1
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+(Y{m,c}((SN*(1-1)+1:SN*(1-1)+SN),:))*b{l}(q_k+2:b_n-SN)+co2pc((SN*(Rr-1)+1:SN*(Rr-1)+SN)).*b{l}(b_n-SN+1:b_n)];
                    end
                    if YF>1
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+(Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:))*b{l}(q_k+2:b_n-SN)+y_hat{c,l}(:,YF-1).*b{l}(b_n-SN+1:b_n)];
                    end
                elseif m>(M-Spl)
                    if YF==1
                        income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(q_k+2:b_n-SN);
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+(Y{m,c}((SN*(1-1)+1:SN*(1-1)+SN),:)).*income_b+co2pc((SN*(Rr-1)+1:SN*(Rr-1)+SN)).*b{l}(b_n-SN+1:b_n)];
                    end
                    if YF>1
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+(Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)).*income_b+y_hat{c,l}(:,YF-1).*b{l}(b_n-SN+1:b_n)];
                    end
                end
                elseif unc==2
                 if m<=(M-Spl)
               
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+(Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:))*b{l}(q_k+2:b_n-SN)+co2pc((SN*(Rr-1-theta+YF)+1:SN*(Rr-1-theta+YF)+SN)).*b{l}(b_n-SN+1:b_n)];
                    
                elseif m>(M-Spl)
                    
                        income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(q_k+2:b_n-SN);
                       
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+(Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)).*income_b+co2pc((SN*(Rr-1-theta+YF)+1:SN*(Rr-1-theta+YF)+SN)).*b{l}(b_n-SN+1:b_n)];
                    
                end
                end

            end
         end
               if logs==2 | logs==4
               y_hat{c,l}=exp(y_hat{c,l}+(e'*e)/(n-k)/2);
               end
    end
end
%---------------------------------

                        
                        if logs==1 | logs==3 
                          f_hat(r,l)=(ones(SN,1)'*[([y_m]-y_hat_m).^2]);
                          f_hatA(r,l)=(ones(SN,1)'*[([y_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))]-y_hat_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))).^2]);
                           
                        elseif logs==2 | logs==4
                            f_hat(r,l)=(ones(SN,1)'*[(exp(y_m)-exp(y_hat_m+(res'*res)/(nu-ka)/2)).^2]);
                            f_hatA(r,l)=(ones(SN,1)'*[(exp(y_m).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))-exp(y_hat_m+(res'*res)/(nu-ka)/2).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))).^2]);
                            
                        end;
                        clear X1 x_time btime y_m b_n res nu ka %was commented out
                        
                    end;
                    
                end;
            end;    
           
% 8st loop INCLUDING Time fixed effects NO STATE FIXED EFFECTS---BUT QUALITY DUMMIES  
            for q=1:QDN
                for years=(Rr-sb):(Rr-sb)+TN-1;
                     %waitbar(l/w)
                    for m=1:MM 
                         l=l+1;
                        for r=1:N
                            Laufzeit=Laufzeit-1;
                            clc
                            disp(['Regressions left: ',num2str(Laufzeit)])
                            
                            
                            if lag==1
                                if m<=(M-Spl)
                                X1=[ones(SN*(R+r),1) QD{q}(1:SN*(R+r),:) time(1:SN*(R+r),2:R+r) X{m}(1:SN*(R+r),:)];
                                elseif m>(M-Spl)
                                X1=[ones(SN*(R+r),1) QD{q}(1:SN*(R+r),:) time(1:SN*(R+r),2:R+r) incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag}];
                                end
                            elseif lag==2
                                if m<=(M-Spl)
                                X1=[ones(SN*(R+r),1) QD{q}(1:SN*(R+r),:) time(1:SN*(R+r),2:R+r) X{m}(1:SN*(R+r),:) LAGco2pc(1:SN*(R+r),:)];
                                elseif m>(M-Spl)
                                 X1=[ones(SN*(R+r),1) QD{q}(1:SN*(R+r),:) time(1:SN*(R+r),2:R+r) incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(R+r),:)];   
                                end
                            end
                            
                            [nu,ka]=size(X1);
                        [b{l},a,res]=regress(co2pc(1:SN*(R+r),:),X1);
                    
                            q_k=size(QD{q},2);
                            b_n=size(b{l},1);
                            
                            %same time trend forecast as above in the benchmark
                            
                            x_time=[ones(R+r-1,1) linspace(1961,1961+R+r-2,R+r-1)' [zeros(years,1)' linspace(0,R+r-2-years,R+r-1-years)]'];
                            btime=regress(b{l}(q_k+2:q_k+1+R+r-1),x_time);
                            
                           y_m=(co2pc(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN));
                            if lag==1
                                if m<=(M-Spl)
                                y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+ones(SN,1).*[(R+r-2-years+theta)*btime(3)+btime(1)+(1961+R+r-2+theta)*btime(2)]+Z{m}((SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN),:)*b{l}(q_k+1+R+r-1+1:b_n)];
                                elseif m>(M-Spl)
                                income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(q_k+1+R+r-1+1:b_n);
                                y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+ones(SN,1).*[(R+r-2-years+theta)*btime(3)+btime(1)+(1961+R+r-2+theta)*btime(2)]+X{m}((SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN),:).*income_b];
                                end 
                            elseif lag==2
                                if unc==1
                                if m<=(M-Spl)
                                y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+ones(SN,1).*[(R+r-2-years+1)*btime(3)+btime(1)+(1961+R+r-2+1)*btime(2)]+Z{m}((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(q_k+1+R+r-1+1:b_n-SN)+LAGco2pc((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                                for t=2:theta
                                y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+ones(SN,1).*[(R+r-2-years+t)*btime(3)+btime(1)+(1961+R+r-2+t)*btime(2)]+Z{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(q_k+1+R+r-1+1:b_n-SN)+y_hat_m.*b{l}(b_n-SN+1:b_n)];
                                end     
                                 elseif m>(M-Spl)
                                income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(q_k+1+R+r-1+1:b_n-SN);
                                y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+ones(SN,1).*[(R+r-2-years+1)*btime(3)+btime(1)+(1961+R+r-2+1)*btime(2)]+X{m}((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:).*income_b+LAGco2pc((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                                for t=2:theta
                                y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+ones(SN,1).*[(R+r-2-years+t)*btime(3)+btime(1)+(1961+R+r-2+t)*btime(2)]+X{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:).*income_b+y_hat_m.*b{l}(b_n-SN+1:b_n)];
                                end  
                                end
                                elseif unc==2
                                                                if m<=(M-Spl)
                                t=theta;
                                y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+ones(SN,1).*[(R+r-2-years+t)*btime(3)+btime(1)+(1961+R+r-2+t)*btime(2)]+Z{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(q_k+1+R+r-1+1:b_n-SN)+LAGco2pc((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                                     
                                 elseif m>(M-Spl)
                                income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(q_k+1+R+r-1+1:b_n-SN);
                               t=theta;
                                y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+ones(SN,1).*[(R+r-2-years+t)*btime(3)+btime(1)+(1961+R+r-2+t)*btime(2)]+X{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:).*income_b+LAGco2pc((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                                                                
                            end
                                end
                                
                            end
%----------------------------------forecasts for each model from Rr+1 bis YEARS+YEARSFOR           
if  r==N
    if lag==1
        if m<=(M-Spl)
            X1=[ones(SN*(Rr),1) QD{q}(1:SN*(Rr),:) time(1:SN*(Rr),2:Rr) X{m}(1:SN*(Rr),:)];
        elseif m>(M-Spl)
            X1=[ones(SN*(Rr),1) QD{q}(1:SN*(Rr),:) time(1:SN*(Rr),2:Rr) incomespline_{N+1,(m-(M-Spl)+(10-Spl)),logs,lag}];
        end
    elseif lag==2
        if m<=(M-Spl)
            X1=[ones(SN*(Rr),1) QD{q}(1:SN*(Rr),:) time(1:SN*(Rr),2:Rr) X{m}(1:SN*(Rr),:) LAGco2pc(1:SN*(Rr),:)];
        elseif m>(M-Spl)
            X1=[ones(SN*(Rr),1) QD{q}(1:SN*(Rr),:) time(1:SN*(Rr),2:Rr) incomespline_{N+1,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(Rr),:)];
        end
    end

clear e n k
            [b{l},a,e,int,stat]=regress(co2pc(1:SN*(Rr),:),X1);
            [n,k]=size(X1);
            %SIC
            SIC(1,l)=[log((e'*e)/(n))+((k/n)*log(n))];
            %AIC
            AIC(1,l)=[log((e'*e)/(n))+(2*k/n)];
            %simple R2
            R_2(1,l)=stat(1,1);
            % adjusted R2
            R2adj(1,l)=1-((n-1)/(n-k))*(1-R_2(1,l));
    q_k=size(QD{q},2);
    b_n=size(b{l},1);

    %same time trend forecast as above in the benchmark

    x_time=[ones(Rr-1,1) linspace(1961,1961+Rr-2,Rr-1)' [zeros(years,1)' linspace(0,Rr-2-years,Rr-1-years)]'];
    btime=regress(b{l}(q_k+2:q_k+1+Rr-1),x_time);

    for c=Cc:CC
        for YF=1:YEARSFOR;
            if lag==1
                if m<=(M-Spl)
                    y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+ones(SN,1).*[(Rr-2-years+YF)*btime(3)+btime(1)+(1961+Rr-2+YF)*btime(2)]+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(q_k+1+Rr-1+1:b_n)];
                elseif m>(M-Spl)
                    income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(q_k+1+Rr-1+1:b_n);
                    y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+ones(SN,1).*[(Rr-2-years+YF)*btime(3)+btime(1)+(1961+Rr-2+YF)*btime(2)]+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b];
                end
            elseif lag==2 
                if unc==1
                if m<=(M-Spl)
                    if YF==1
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+ones(SN,1).*[(Rr-2-years+1)*btime(3)+btime(1)+(1961+Rr-2+1)*btime(2)]+Y{m,c}((SN*(1-1)+1:SN*(1-1)+SN),:)*b{l}(q_k+1+Rr-1+1:b_n-SN)+co2pc((SN*(Rr-1)+1:SN*(Rr-1)+SN)).*b{l}(b_n-SN+1:b_n)];
                    end
                    if YF>1
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+ones(SN,1).*[(Rr-2-years+YF)*btime(3)+btime(1)+(1961+Rr-2+YF)*btime(2)]+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(q_k+1+Rr-1+1:b_n-SN)+y_hat{c,l}(:,YF-1).*b{l}(b_n-SN+1:b_n)];
                    end
                elseif m>(M-Spl)
                    if YF==1
                        income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(q_k+1+Rr-1+1:b_n-SN);
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+ones(SN,1).*[(Rr-2-years+1)*btime(3)+btime(1)+(1961+Rr-2+1)*btime(2)]+Y{m,c}((SN*(1-1)+1:SN*(1-1)+SN),:).*income_b+co2pc((SN*(Rr-1)+1:SN*(Rr-1)+SN)).*b{l}(b_n-SN+1:b_n)];
                    end
                    if YF>1
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+ones(SN,1).*[(Rr-2-years+YF)*btime(3)+btime(1)+(1961+Rr-2+YF)*btime(2)]+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b+y_hat{c,l}(:,YF-1).*b{l}(b_n-SN+1:b_n)];
                    end
                end
                elseif unc==2
                if m<=(M-Spl)
                   
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+ones(SN,1).*[(Rr-2-years+YF)*btime(3)+btime(1)+(1961+Rr-2+YF)*btime(2)]+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(q_k+1+Rr-1+1:b_n-SN)+co2pc((SN*(Rr-1-theta+YF)+1:SN*(Rr-1-theta+YF)+SN)).*b{l}(b_n-SN+1:b_n)];
                    
                elseif m>(M-Spl)
                   
                        income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(q_k+1+Rr-1+1:b_n-SN);
                   
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+ones(SN,1).*[(Rr-2-years+YF)*btime(3)+btime(1)+(1961+Rr-2+YF)*btime(2)]+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b+co2pc((SN*(Rr-1-theta+YF)+1:SN*(Rr-1-theta+YF)+SN)).*b{l}(b_n-SN+1:b_n)];
                    
                end
                end
                
            end
         end
               if logs==2 | logs==4
               y_hat{c,l}=exp(y_hat{c,l}+(e'*e)/(n-k)/2);
               end
    end
end
%---------------------------------

                            
                            if logs==1 | logs==3 
                                f_hat(r,l)=(ones(SN,1)'*[([y_m]-y_hat_m).^2]);
                                f_hatA(r,l)=(ones(SN,1)'*[([y_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))]-y_hat_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))).^2]);
                                
                            elseif logs==2 | logs==4
                                f_hat(r,l)=(ones(SN,1)'*[(exp(y_m)-exp(y_hat_m+(res'*res)/(nu-ka)/2)).^2]);
                                f_hatA(r,l)=(ones(SN,1)'*[(exp(y_m).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))-exp(y_hat_m+(res'*res)/(nu-ka)/2).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))).^2]);
                                
                            end;
                            clear X1 x_time btime y_m b_n res nu ka 
                        end;
                        
                    end;
                    
                end;
            end;
         
% 9th loop INCLUDING TIMETREND      NO STATE FIXED EFFECTS---BUT QUALITY DUMMIES 
            for q=1:QDN    
                %waitbar(l/w)
                for m=1:MM   
                     l=l+1;
                    for r=1:N
                        Laufzeit=Laufzeit-1;
                        clc
                        disp(['Regressions left: ',num2str(Laufzeit)])
                        
                        
                        if lag==1
                            if m<=(M-Spl)
                            X1=[ones(SN*(R+r),1) QD{q}(1:SN*(R+r),:) timetrend(1:SN*(R+r)) X{m}(1:SN*(R+r),:)];
                            elseif m>(M-Spl)
                                 X1=[ones(SN*(R+r),1) QD{q}(1:SN*(R+r),:) timetrend(1:SN*(R+r)) incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag}];
                                end
                        elseif lag==2
                            if m<=(M-Spl)
                            X1=[ones(SN*(R+r),1) QD{q}(1:SN*(R+r),:) timetrend(1:SN*(R+r)) X{m}(1:SN*(R+r),:) LAGco2pc(1:SN*(R+r),:)];
                             elseif m>(M-Spl)
                              X1=[ones(SN*(R+r),1) QD{q}(1:SN*(R+r),:) timetrend(1:SN*(R+r)) incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(R+r),:)];    
                             end
                        end
                        [nu,ka]=size(X1);
                        [b{l},a,res]=regress(co2pc(1:SN*(R+r),:),X1);
                   
                        b_n=size(b{l},1);
                        q_k=size(QD{q},2);
                        
                       y_m=(co2pc(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN));
                        if lag==1
                            if m<=(M-Spl)
                            y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+timetrend(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN).*b{l}(q_k+2)+Z{m}((SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN),:)*b{l}(q_k+2+1:b_n)];
                            elseif m>(M-Spl)
                                income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(q_k+2+1:b_n);
                               y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+timetrend(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN).*b{l}(q_k+2)+X{m}((SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN),:).*income_b];
                                end 
                        elseif lag==2
                            if unc==1
                            if m<=(M-Spl)
                            y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+timetrend(SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN).*b{l}(q_k+2)+Z{m}((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(q_k+2+1:b_n-SN)+LAGco2pc((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                            for t=2:theta
                            y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+timetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(q_k+2)+Z{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(q_k+2+1:b_n-SN)+y_hat_m.*b{l}(b_n-SN+1:b_n)];
                            end     
                             elseif m>(M-Spl)
                                income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(q_k+2+1:b_n-SN);
                                 y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+timetrend(SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN).*b{l}(q_k+2)+X{m}((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:).*income_b+LAGco2pc((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                            for t=2:theta
                            y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+timetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(q_k+2)+X{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:).*income_b+y_hat_m.*b{l}(b_n-SN+1:b_n)];
                            end     
                            end
                            elseif unc==2
                                                        if m<=(M-Spl)
                            t=theta;
                            y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+timetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(q_k+2)+Z{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(q_k+2+1:b_n-SN)+LAGco2pc((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                                 
                             elseif m>(M-Spl)
                                income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(q_k+2+1:b_n-SN);
                            t=theta;
                            y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+timetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(q_k+2)+X{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:).*income_b+LAGco2pc((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                                 
                        end
                            end
                            
                        end
%----------------------------------forecasts for each model from Rr+1 bis YEARS+YEARSFOR           
if  r==N
    if lag==1
        if m<=(M-Spl)
            X1=[ones(SN*(Rr),1) QD{q}(1:SN*(Rr),:) timetrend(1:SN*(Rr)) X{m}(1:SN*(Rr),:)];
        elseif m>(M-Spl)
            X1=[ones(SN*(Rr),1) QD{q}(1:SN*(Rr),:) timetrend(1:SN*(Rr)) incomespline_{N+1,(m-(M-Spl)+(10-Spl)),logs,lag}];
        end
    elseif lag==2
        if m<=(M-Spl)
            X1=[ones(SN*(Rr),1) QD{q}(1:SN*(Rr),:) timetrend(1:SN*(Rr)) X{m}(1:SN*(Rr),:) LAGco2pc(1:SN*(Rr),:)];
        elseif m>(M-Spl)
            X1=[ones(SN*(Rr),1) QD{q}(1:SN*(Rr),:) timetrend(1:SN*(Rr)) incomespline_{N+1,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(Rr),:)];
        end
    end
clear e n k
            [b{l},a,e,int,stat]=regress(co2pc(1:SN*(Rr),:),X1);
            [n,k]=size(X1);
            %SIC
            SIC(1,l)=[log((e'*e)/(n))+((k/n)*log(n))];
            %AIC
            AIC(1,l)=[log((e'*e)/(n))+(2*k/n)];
            %simple R2
            R_2(1,l)=stat(1,1);
            % adjusted R2
            R2adj(1,l)=1-((n-1)/(n-k))*(1-R_2(1,l));
    b_n=size(b{l},1);
    q_k=size(QD{q},2);

    for c=Cc:CC
        for YF=1:YEARSFOR;
            if lag==1
                if m<=(M-Spl)
                    y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+timetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(q_k+2)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(q_k+2+1:b_n)];
                elseif m>(M-Spl)
                    income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(q_k+2+1:b_n);
                    y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+timetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(q_k+2)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b];
                end
            elseif lag==2
               if unc==1
                if m<=(M-Spl)
                    if YF==1
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+timetrend(SN*(Rr+1-1)+1:SN*(Rr+1-1)+SN).*b{l}(q_k+2)+Y{m,c}((SN*(1-1)+1:SN*(1-1)+SN),:)*b{l}(q_k+2+1:b_n-SN)+co2pc((SN*(Rr-1)+1:SN*(Rr-1)+SN)).*b{l}(b_n-SN+1:b_n)];
                    end
                    if YF>1
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+timetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(q_k+2)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(q_k+2+1:b_n-SN)+y_hat{c,l}(:,YF-1).*b{l}(b_n-SN+1:b_n)];
                    end
                elseif m>(M-Spl)
                    if YF==1
                        income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(q_k+2+1:b_n-SN);
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+timetrend(SN*(Rr+1-1)+1:SN*(Rr+1-1)+SN).*b{l}(q_k+2)+Y{m,c}((SN*(1-1)+1:SN*(1-1)+SN),:).*income_b+co2pc((SN*(Rr-1)+1:SN*(Rr-1)+SN)).*b{l}(b_n-SN+1:b_n)];
                    end
                    if YF>1
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+timetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(q_k+2)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b+y_hat{c,l}(:,YF-1).*b{l}(b_n-SN+1:b_n)];
                    end
                end
               elseif unc==2
                 if m<=(M-Spl)
                    
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+timetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(q_k+2)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(q_k+2+1:b_n-SN)+co2pc((SN*(Rr-1-theta+YF)+1:SN*(Rr-1-theta+YF)+SN)).*b{l}(b_n-SN+1:b_n)];
                    
                elseif m>(M-Spl)
                    
                        income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(q_k+2+1:b_n-SN);
                      
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+timetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(q_k+2)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b+co2pc((SN*(Rr-1-theta+YF)+1:SN*(Rr-1-theta+YF)+SN)).*b{l}(b_n-SN+1:b_n)];
                    
                end
               end
               
            end
         end
               if logs==2 | logs==4
               y_hat{c,l}=exp(y_hat{c,l}+(e'*e)/(n-k)/2);
               end
    end

end
%---------------------------------

                        
                        if logs==1 | logs==3 
                            f_hat(r,l)=(ones(SN,1)'*[([y_m]-y_hat_m).^2]);
                            f_hatA(r,l)=(ones(SN,1)'*[([y_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))]-y_hat_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))).^2]);
                            
                        elseif logs==2 | logs==4
                            f_hat(r,l)=(ones(SN,1)'*[(exp(y_m)-exp(y_hat_m+(res'*res)/(nu-ka)/2)).^2]);
                            f_hatA(r,l)=(ones(SN,1)'*[(exp(y_m).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))-exp(y_hat_m+(res'*res)/(nu-ka)/2).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))).^2]);
                            
                        end;
                        clear X1 x_time btime y_m b_n res nu ka 
                    end;
                    
                end;
            end;
            
% 10rd loop INCLUDING State fixed effects  and LOG-TIMETREND   
             %waitbar(l/w)
            for m=1:MM    
                 l=l+1;
                for r=1:N
                    Laufzeit=Laufzeit-1;
                    clc
                    disp(['Regressions left: ',num2str(Laufzeit)])
                    
                    
                    if lag==1
                        if m<=(M-Spl)
                        X1=[state(1:SN*(R+r),:) LOGtimetrend(1:SN*(R+r)) X{m}(1:SN*(R+r),:)];
                        elseif m>(M-Spl)
                                 X1=[state(1:SN*(R+r),:) LOGtimetrend(1:SN*(R+r)) incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag}];
                                end
                    elseif lag==2
                        if m<=(M-Spl)
                        X1=[state(1:SN*(R+r),:) LOGtimetrend(1:SN*(R+r)) X{m}(1:SN*(R+r),:) LAGco2pc(1:SN*(R+r),:)]; 
                        elseif m>(M-Spl)
                         X1=[state(1:SN*(R+r),:) LOGtimetrend(1:SN*(R+r)) incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(R+r),:)];     
                        end
                    end
                    [nu,ka]=size(X1);
                        [b{l},a,res]=regress(co2pc(1:SN*(R+r),:),X1);
                            
                    b_n=size(b{l},1);
                    
                    y_m=(co2pc(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN));
                    if lag==1
                        if m<=(M-Spl)
                        y_hat_m=[eye(SN,SN)*b{l}(1:SN)+LOGtimetrend(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN).*b{l}(SN+1)+Z{m}((SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN),:)*b{l}(SN+1+1:b_n)];
                        elseif m>(M-Spl)
                                income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(SN+1+1:b_n);
                              y_hat_m=[eye(SN,SN)*b{l}(1:SN)+LOGtimetrend(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN).*b{l}(SN+1)+X{m}((SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN),:).*income_b];
                                end 
                    elseif lag==2
                        if unc==1
                        if m<=(M-Spl)
                        y_hat_m=[eye(SN,SN)*b{l}(1:SN)+LOGtimetrend(SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN).*b{l}(SN+1)+Z{m}((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(SN+1+1:b_n-SN)+LAGco2pc((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                        for t=2:theta
                        y_hat_m=[eye(SN,SN)*b{l}(1:SN)+LOGtimetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(SN+1)+Z{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(SN+1+1:b_n-SN)+y_hat_m.*b{l}(b_n-SN+1:b_n)];
                        end        
                         elseif m>(M-Spl)
                                income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(SN+1+1:b_n-SN);
                        y_hat_m=[eye(SN,SN)*b{l}(1:SN)+LOGtimetrend(SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN).*b{l}(SN+1)+X{m}((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:).*income_b+LAGco2pc((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                        for t=2:theta
                        y_hat_m=[eye(SN,SN)*b{l}(1:SN)+LOGtimetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(SN+1)+X{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:).*income_b+y_hat_m.*b{l}(b_n-SN+1:b_n)];
                        end  
                        end
                        elseif unc==2
                                                if m<=(M-Spl)
                        t=theta;
                        y_hat_m=[eye(SN,SN)*b{l}(1:SN)+LOGtimetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(SN+1)+Z{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(SN+1+1:b_n-SN)+LAGco2pc((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                                
                         elseif m>(M-Spl)
                                income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(SN+1+1:b_n-SN);
                        t=theta;
                        y_hat_m=[eye(SN,SN)*b{l}(1:SN)+LOGtimetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(SN+1)+X{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:).*income_b+LAGco2pc((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                          
                    end
                        end
                    end
%----------------------------------forecasts for each model from Rr+1 bis YEARS+YEARSFOR           
if  r==N
    if lag==1
        if m<=(M-Spl)
            X1=[state(1:SN*(Rr),:) LOGtimetrend(1:SN*(Rr)) X{m}(1:SN*(Rr),:)];
        elseif m>(M-Spl)
            X1=[state(1:SN*(Rr),:) LOGtimetrend(1:SN*(Rr)) incomespline_{N+1,(m-(M-Spl)+(10-Spl)),logs,lag}];
        end
    elseif lag==2
        if m<=(M-Spl)
            X1=[state(1:SN*(Rr),:) LOGtimetrend(1:SN*(Rr)) X{m}(1:SN*(Rr),:) LAGco2pc(1:SN*(Rr),:)];
        elseif m>(M-Spl)
            X1=[state(1:SN*(Rr),:) LOGtimetrend(1:SN*(Rr)) incomespline_{N+1,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(Rr),:)];
        end
    end
clear e n k
            [b{l},a,e,int,stat]=regress(co2pc(1:SN*(Rr),:),X1);
            [n,k]=size(X1);
            %SIC
            SIC(1,l)=[log((e'*e)/(n))+((k/n)*log(n))];
            %AIC
            AIC(1,l)=[log((e'*e)/(n))+(2*k/n)];
            %simple R2
            R_2(1,l)=stat(1,1);
            % adjusted R2
            R2adj(1,l)=1-((n-1)/(n-k))*(1-R_2(1,l));
    b_n=size(b{l},1);

    for c=Cc:CC
        for YF=1:YEARSFOR;
            if lag==1
                if m<=(M-Spl)
                    y_hat{c,l}(:,YF)=[b{l}(1:SN)+LOGtimetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(SN+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(SN+1+1:b_n)];
                elseif m>(M-Spl)
                    income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(SN+1+1:b_n);
                    y_hat{c,l}(:,YF)=[b{l}(1:SN)+LOGtimetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(SN+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b];
                end
            elseif lag==2  
               if unc==1
                if m<=(M-Spl)
                    if YF==1
                        y_hat{c,l}(:,YF)=[b{l}(1:SN)+LOGtimetrend(SN*(Rr+1-1)+1:SN*(Rr+1-1)+SN).*b{l}(SN+1)+Y{m,c}((SN*(1-1)+1:SN*(1-1)+SN),:)*b{l}(SN+1+1:b_n-SN)+co2pc((SN*(Rr-1)+1:SN*(Rr-1)+SN)).*b{l}(b_n-SN+1:b_n)];
                    end
                    if YF>1
                        y_hat{c,l}(:,YF)=[b{l}(1:SN)+LOGtimetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(SN+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(SN+1+1:b_n-SN)+y_hat{c,l}(:,YF-1).*b{l}(b_n-SN+1:b_n)];
                    end
                elseif m>(M-Spl)
                    if YF==1
                        income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(SN+1+1:b_n-SN);
                        y_hat{c,l}(:,YF)=[b{l}(1:SN)+LOGtimetrend(SN*(Rr+1-1)+1:SN*(Rr+1-1)+SN).*b{l}(SN+1)+Y{m,c}((SN*(1-1)+1:SN*(1-1)+SN),:).*income_b+co2pc((SN*(Rr-1)+1:SN*(Rr-1)+SN)).*b{l}(b_n-SN+1:b_n)];
                    end
                    if YF>1
                        y_hat{c,l}(:,YF)=[b{l}(1:SN)+LOGtimetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(SN+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b+y_hat{c,l}(:,YF-1).*b{l}(b_n-SN+1:b_n)];
                    end
                end
               elseif unc==2
               if m<=(M-Spl)
                   
                        y_hat{c,l}(:,YF)=[b{l}(1:SN)+LOGtimetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(SN+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(SN+1+1:b_n-SN)+co2pc((SN*(Rr-1-theta+YF)+1:SN*(Rr-1-theta+YF)+SN)).*b{l}(b_n-SN+1:b_n)];
                    
                elseif m>(M-Spl)
                    
                        income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(SN+1+1:b_n-SN);
                    
                        y_hat{c,l}(:,YF)=[b{l}(1:SN)+LOGtimetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(SN+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b+co2pc((SN*(Rr-1-theta+YF)+1:SN*(Rr-1-theta+YF)+SN)).*b{l}(b_n-SN+1:b_n)];
                   
                end
               end
               
            end
         end
               if logs==2 | logs==4
               y_hat{c,l}=exp(y_hat{c,l}+(e'*e)/(n-k)/2);
               end
    end

end
%---------------------------------
                    
                    if logs==1 | logs==3 
                        f_hat(r,l)=(ones(SN,1)'*[([y_m]-y_hat_m).^2]);
                        f_hatA(r,l)=(ones(SN,1)'*[([y_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))]-y_hat_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))).^2]);
                       
                    elseif logs==2  | logs==4
                        f_hat(r,l)=(ones(SN,1)'*[(exp(y_m)-exp(y_hat_m+(res'*res)/(nu-ka)/2)).^2]);
                        f_hatA(r,l)=(ones(SN,1)'*[(exp(y_m).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))-exp(y_hat_m+(res'*res)/(nu-ka)/2).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))).^2]);
                        
                    end;
                    clear X1 x_time btime y_m b_n res nu ka 
                end;
                
            end;
            
            
% 11th loop INCLUDING LOG-TIMETREND      NO STATE FIXED EFFECTS 
%INCLUDING US AGG REGRESSIONS 
            for m=1:MM 
                 l=l+1;
                for r=1:N
                    Laufzeit=Laufzeit-1;
                    clc
                    disp(['Regressions left: ',num2str(Laufzeit)])
                    
                    
                    if lag==1
                        if m<=(M-Spl)
                        X1=[ones(SN*(R+r),1) LOGtimetrend(1:SN*(R+r)) X{m}(1:SN*(R+r),:)];
                                    X1A=[ones(1*(R+r),1) LOGtimetrendA(1:1*(R+r)) XA{m}(1:1*(R+r),:)];
                        elseif m>(M-Spl)
                                 X1=[ones(SN*(R+r),1) LOGtimetrend(1:SN*(R+r)) incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag}];
                                end
                    elseif lag==2
                        if m<=(M-Spl)
                        X1=[ones(SN*(R+r),1) LOGtimetrend(1:SN*(R+r)) X{m}(1:SN*(R+r),:) LAGco2pc(1:SN*(R+r),:)]; 
                                         X1A=[ones(1*(R+r),1) LOGtimetrendA(1:1*(R+r)) XA{m}(1:1*(R+r),:) LAGco2pcA(1:1*(R+r),:)]; 
                        elseif m>(M-Spl)
                         X1=[ones(SN*(R+r),1) LOGtimetrend(1:SN*(R+r)) incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(R+r),:)];    
                        end
                    end
                    [nu,ka]=size(X1);
                        [b{l},a,res]=regress(co2pc(1:SN*(R+r),:),X1);
                             
                    b_n=size(b{l},1);
                    
                    y_m=(co2pc(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN));
                    if m<=(M-Spl)
                                            [nuA,kaA]=size(X1A);
                                                [bA{l},aA,resA]=regress(co2pcA(1:1*(R+r),:),X1A);
                                                     
                                            b_nA=size(bA{l},1);

                                            y_mA=(co2pcA(1*(R+r+theta-1)+1:1*(R+r+theta-1)+1));
                    end
                    if lag==1
                        if m<=(M-Spl)
                        y_hat_m=[ones(SN,1).*b{l}(1)+LOGtimetrend(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN).*b{l}(1+1)+Z{m}((SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN),:)*b{l}(1+1+1:b_n)];
                                        y_hat_mA=[ones(1,1).*bA{l}(1)+LOGtimetrendA(1*(R+r+theta-1)+1:1*(R+r+theta-1)+1).*bA{l}(1+1)+ZA{m}((1*(R+r+theta-1)+1:1*(R+r+theta-1)+1),:)*bA{l}(1+1+1:b_nA)];
                        elseif m>(M-Spl)
                                income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(1+1+1:b_n);
                              y_hat_m=[ones(SN,1).*b{l}(1)+LOGtimetrend(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN).*b{l}(1+1)+X{m}((SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN),:).*income_b];
                                end 
                    elseif lag==2
                        if unc==1
                        if m<=(M-Spl)
                        y_hat_m=[ones(SN,1).*b{l}(1)+LOGtimetrend(SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN).*b{l}(1+1)+Z{m}((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(1+1+1:b_n-SN)+LAGco2pc((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                        for t=2:theta
                        y_hat_m=[ones(SN,1).*b{l}(1)+LOGtimetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(1+1)+Z{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(1+1+1:b_n-SN)+y_hat_m.*b{l}(b_n-SN+1:b_n)];
                        end  
                                            y_hat_mA=[ones(1,1).*bA{l}(1)+LOGtimetrendA(1*(R+r+1-1)+1:1*(R+r+1-1)+1).*bA{l}(1+1)+ZA{m}((1*(R+r+1-1)+1:1*(R+r+1-1)+1),:)*bA{l}(1+1+1:b_nA-1)+LAGco2pcA((1*(R+r+1-1)+1:1*(R+r+1-1)+1),:)*bA{l}(b_nA-1+1:b_nA)];
                                            for t=2:theta
                                            y_hat_mA=[ones(1,1).*bA{l}(1)+LOGtimetrendA(1*(R+r+t-1)+1:1*(R+r+t-1)+1).*bA{l}(1+1)+ZA{m}((1*(R+r+t-1)+1:1*(R+r+t-1)+1),:)*bA{l}(1+1+1:b_nA-1)+y_hat_mA.*bA{l}(b_nA-1+1:b_nA)];
                                            end
                         elseif m>(M-Spl)
                                income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(1+1+1:b_n-SN);
                         y_hat_m=[ones(SN,1).*b{l}(1)+LOGtimetrend(SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN).*b{l}(1+1)+X{m}((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:).*income_b+LAGco2pc((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                        for t=2:theta
                        y_hat_m=[ones(SN,1).*b{l}(1)+LOGtimetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(1+1)+X{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:).*income_b+y_hat_m.*b{l}(b_n-SN+1:b_n)];
                        end
                        end
                        elseif unc==2
                                                if m<=(M-Spl)
                        t=theta;
                        y_hat_m=[ones(SN,1).*b{l}(1)+LOGtimetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(1+1)+Z{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(1+1+1:b_n-SN)+LAGco2pc((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                          
                                            t=theta;
                                            y_hat_mA=[ones(1,1).*bA{l}(1)+LOGtimetrendA(1*(R+r+t-1)+1:1*(R+r+t-1)+1).*bA{l}(1+1)+ZA{m}((1*(R+r+t-1)+1:1*(R+r+t-1)+1),:)*bA{l}(1+1+1:b_nA-1)+LAGco2pcA((1*(R+r+t-1)+1:1*(R+r+t-1)+1),:)*bA{l}(b_nA-1+1:b_nA)];
                                            
                         elseif m>(M-Spl)
                                income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(1+1+1:b_n-SN);
                        t=theta;
                        y_hat_m=[ones(SN,1).*b{l}(1)+LOGtimetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(1+1)+X{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:).*income_b+LAGco2pc((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                        
                    end
                        end
                    end
%----------------------------------forecasts for each model from Rr+1 bis YEARS+YEARSFOR           
if  r==N
    if lag==1
        if m<=(M-Spl)
            X1=[ones(SN*(Rr),1) LOGtimetrend(1:SN*(Rr)) X{m}(1:SN*(Rr),:)];
        elseif m>(M-Spl)
            X1=[ones(SN*(Rr),1) LOGtimetrend(1:SN*(Rr)) incomespline_{N+1,(m-(M-Spl)+(10-Spl)),logs,lag}];
        end
    elseif lag==2
        if m<=(M-Spl)
            X1=[ones(SN*(Rr),1) LOGtimetrend(1:SN*(Rr)) X{m}(1:SN*(Rr),:) LAGco2pc(1:SN*(Rr),:)];
        elseif m>(M-Spl)
            X1=[ones(SN*(Rr),1) LOGtimetrend(1:SN*(Rr)) incomespline_{N+1,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(Rr),:)];
        end
    end
clear e n k
            [b{l},a,e,int,stat]=regress(co2pc(1:SN*(Rr),:),X1);
            [n,k]=size(X1);
            %SIC
            SIC(1,l)=[log((e'*e)/(n))+((k/n)*log(n))];
            %AIC
            AIC(1,l)=[log((e'*e)/(n))+(2*k/n)];
            %simple R2
            R_2(1,l)=stat(1,1);
            % adjusted R2
            R2adj(1,l)=1-((n-1)/(n-k))*(1-R_2(1,l));
    b_n=size(b{l},1);

    for c=Cc:CC
        for YF=1:YEARSFOR;
            if lag==1
                if m<=(M-Spl)
                    y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+LOGtimetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(1+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(1+1+1:b_n)];
                elseif m>(M-Spl)
                    income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(1+1+1:b_n);
                    y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+LOGtimetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(1+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b];
                end
            elseif lag==2  
                if unc==1
                if m<=(M-Spl)
                    if YF==1
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+LOGtimetrend(SN*(Rr+1-1)+1:SN*(Rr+1-1)+SN).*b{l}(1+1)+Y{m,c}((SN*(1-1)+1:SN*(1-1)+SN),:)*b{l}(1+1+1:b_n-SN)+co2pc((SN*(Rr-1)+1:SN*(Rr-1)+SN)).*b{l}(b_n-SN+1:b_n)];
                    end
                    if YF>1
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+LOGtimetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(1+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(1+1+1:b_n-SN)+y_hat{c,l}(:,YF-1).*b{l}(b_n-SN+1:b_n)];
                    end
                elseif m>(M-Spl)
                    if YF==1
                        income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(1+1+1:b_n-SN);
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+LOGtimetrend(SN*(Rr+1-1)+1:SN*(Rr+1-1)+SN).*b{l}(1+1)+Y{m,c}((SN*(1-1)+1:SN*(1-1)+SN),:).*income_b+co2pc((SN*(Rr-1)+1:SN*(Rr-1)+SN)).*b{l}(b_n-SN+1:b_n)];
                    end
                    if YF>1
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+LOGtimetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(1+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b+y_hat{c,l}(:,YF-1).*b{l}(b_n-SN+1:b_n)];
                    end
                end
                elseif unc==2
                if m<=(M-Spl)
                   
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+LOGtimetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(1+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(1+1+1:b_n-SN)+co2pc((SN*(Rr-1-theta+YF)+1:SN*(Rr-1-theta+YF)+SN)).*b{l}(b_n-SN+1:b_n)];
                    
                elseif m>(M-Spl)
                    
                        income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(1+1+1:b_n-SN);
                    
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+LOGtimetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(1+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b+co2pc((SN*(Rr-1-theta+YF)+1:SN*(Rr-1-theta+YF)+SN)).*b{l}(b_n-SN+1:b_n)];
                    
                end
                end
            end
         end
               if logs==2 | logs==4
               y_hat{c,l}=exp(y_hat{c,l}+(e'*e)/(n-k)/2);
               end
    end

end
%---------------------------------
                    
                    if logs==1 | logs==3 
                        f_hat(r,l)=(ones(SN,1)'*[([y_m]-y_hat_m).^2]);
                        f_hatA(r,l)=(ones(SN,1)'*[([y_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))]-y_hat_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))).^2]);
                      
                       if m<=(M-Spl) 
                       f_hatagg(r,l)=[(((ones(SN,1)'*[y_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))])/popA(1*(R+r+theta-1)+1:1*(R+r+theta)))-((ones(SN,1)'*(y_hat_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))))/popA(1*(R+r+theta-1)+1:1*(R+r+theta)))).^2];
                        f_hatAagg(r,l)=([(ones(SN,1)'*[y_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))]-(ones(SN,1)'*(y_hat_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))))).^2]);
                        
                        f_hataggA(r,l)=([([y_mA]-y_hat_mA).^2]);
                        f_hatAaggA(r,l)=([([y_mA.*popA(1*(R+r+theta-1)+1:1*(R+r+theta))]-y_hat_mA.*popA(1*(R+r+theta-1)+1:1*(R+r+theta))).^2]);
                       end
                       elseif logs==2  | logs==4
                        f_hat(r,l)=(ones(SN,1)'*[(exp(y_m)-exp(y_hat_m+(res'*res)/(nu-ka)/2)).^2]);
                        f_hatA(r,l)=(ones(SN,1)'*[(exp(y_m).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))-exp(y_hat_m+(res'*res)/(nu-ka)/2).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))).^2]);
                        
                        if m<=(M-Spl)
                        f_hatagg(r,l)=[(((ones(SN,1)'*[exp(y_m).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))])/popA(1*(R+r+theta-1)+1:1*(R+r+theta)))-((ones(SN,1)'*(exp(y_hat_m+(res'*res)/(nu-ka)/2).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))))/popA(1*(R+r+theta-1)+1:1*(R+r+theta)))).^2];
                        f_hatAagg(r,l)=([(ones(SN,1)'*[exp(y_m).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))]-(ones(SN,1)'*(exp(y_hat_m+(res'*res)/(nu-ka)/2).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))))).^2]);
                        
                        f_hataggA(r,l)=([([exp(y_mA)]-exp(y_hat_mA+(resA'*resA)/(nuA-kaA)/2)).^2]);
                        f_hatAaggA(r,l)=([([exp(y_mA).*popA(1*(R+r+theta-1)+1:1*(R+r+theta))]-exp(y_hat_mA+(resA'*resA)/(nuA-kaA)/2).*popA(1*(R+r+theta-1)+1:1*(R+r+theta))).^2]);
                        end
                        end;
                    clear X1 x_time btime y_m b_n res nu ka y_hat_m y_hat_mA y_mA b_nA resA nuA kaA 
                end;
                
            end;
% 12th loop INCLUDING LOG-TIMETREND      NO STATE FIXED EFFECTS---BUT QUALITY DUMMIES 
            for q=1:QDN    
                 %waitbar(l/w)
                for m=1:MM 
                     l=l+1;
                    for r=1:N
                        Laufzeit=Laufzeit-1;
                        clc
                        disp(['Regressions left: ',num2str(Laufzeit)])
                        
                        
                        if lag==1
                            if m<=(M-Spl)
                            X1=[ones(SN*(R+r),1) QD{q}(1:SN*(R+r),:) LOGtimetrend(1:SN*(R+r)) X{m}(1:SN*(R+r),:)];
                            elseif m>(M-Spl)
                                 X1=[ones(SN*(R+r),1) QD{q}(1:SN*(R+r),:) LOGtimetrend(1:SN*(R+r)) incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag}];
                                end
                        elseif lag==2
                            if m<=(M-Spl)
                            X1=[ones(SN*(R+r),1) QD{q}(1:SN*(R+r),:) LOGtimetrend(1:SN*(R+r)) X{m}(1:SN*(R+r),:) LAGco2pc(1:SN*(R+r),:)];
                            elseif m>(M-Spl)
                            X1=[ones(SN*(R+r),1) QD{q}(1:SN*(R+r),:) LOGtimetrend(1:SN*(R+r)) incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(R+r),:)];    
                            end
                        end
                        [nu,ka]=size(X1);
                        [b{l},a,res]=regress(co2pc(1:SN*(R+r),:),X1);
                  
                        b_n=size(b{l},1);
                        q_k=size(QD{q},2);
                        
                        y_m=(co2pc(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN));
                        if lag==1
                            if m<=(M-Spl)
                            y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+LOGtimetrend(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN).*b{l}(q_k+2)+Z{m}((SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN),:)*b{l}(q_k+2+1:b_n)];
                            elseif m>(M-Spl)
                                income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(q_k+2+1:b_n);
                              y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+LOGtimetrend(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN).*b{l}(q_k+2)+X{m}((SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN),:).*income_b];
                                end 
                        elseif lag==2
                            if unc==1
                            if m<=(M-Spl)
                            y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+LOGtimetrend(SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN).*b{l}(q_k+2)+Z{m}((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(q_k+2+1:b_n-SN)+LAGco2pc((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                            for t=2:theta
                            y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+LOGtimetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(q_k+2)+Z{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(q_k+2+1:b_n-SN)+y_hat_m.*b{l}(b_n-SN+1:b_n)];
                            end     
                             elseif m>(M-Spl)
                                income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(q_k+2+1:b_n-SN);
                            y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+LOGtimetrend(SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN).*b{l}(q_k+2)+X{m}((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:).*income_b+LAGco2pc((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                            for t=2:theta
                            y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+LOGtimetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(q_k+2)+X{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:).*income_b+y_hat_m.*b{l}(b_n-SN+1:b_n)];
                            end  
                            end
                            elseif unc==2
                                                        if m<=(M-Spl)
                            t=theta;
                            y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+LOGtimetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(q_k+2)+Z{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(q_k+2+1:b_n-SN)+LAGco2pc((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                                 
                             elseif m>(M-Spl)
                                income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(q_k+2+1:b_n-SN);
                            t=theta;
                            y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+LOGtimetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(q_k+2)+X{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:).*income_b+LAGco2pc((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                              
                        end
                            end
                            
                        end
%----------------------------------forecasts for each model from Rr+1 bis YEARS+YEARSFOR           
if  r==N
    if lag==1
        if m<=(M-Spl)
            X1=[ones(SN*(Rr),1) QD{q}(1:SN*(Rr),:) LOGtimetrend(1:SN*(Rr)) X{m}(1:SN*(Rr),:)];
        elseif m>(M-Spl)
            X1=[ones(SN*(Rr),1) QD{q}(1:SN*(Rr),:) LOGtimetrend(1:SN*(Rr)) incomespline_{N+1,(m-(M-Spl)+(10-Spl)),logs,lag}];
        end
    elseif lag==2
        if m<=(M-Spl)
            X1=[ones(SN*(Rr),1) QD{q}(1:SN*(Rr),:) LOGtimetrend(1:SN*(Rr)) X{m}(1:SN*(Rr),:) LAGco2pc(1:SN*(Rr),:)];
        elseif m>(M-Spl)
            X1=[ones(SN*(Rr),1) QD{q}(1:SN*(Rr),:) LOGtimetrend(1:SN*(Rr)) incomespline_{N+1,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(Rr),:)];
        end
    end
clear e n k
            [b{l},a,e,int,stat]=regress(co2pc(1:SN*(Rr),:),X1);
            [n,k]=size(X1);
            %SIC
            SIC(1,l)=[log((e'*e)/(n))+((k/n)*log(n))];
            %AIC
            AIC(1,l)=[log((e'*e)/(n))+(2*k/n)];
            %simple R2
            R_2(1,l)=stat(1,1);
            % adjusted R2
            R2adj(1,l)=1-((n-1)/(n-k))*(1-R_2(1,l));
    b_n=size(b{l},1);
    q_k=size(QD{q},2);

    for c=Cc:CC
        for YF=1:YEARSFOR;
            if lag==1
                if m<=(M-Spl)
                    y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+LOGtimetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(q_k+2)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(q_k+2+1:b_n)];
                elseif m>(M-Spl)
                    income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(q_k+2+1:b_n);
                    y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+LOGtimetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(q_k+2)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b];
                end
            elseif lag==2 
              if unc==1
                if m<=(M-Spl)
                    if YF==1
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+LOGtimetrend(SN*(Rr+1-1)+1:SN*(Rr+1-1)+SN).*b{l}(q_k+2)+Y{m,c}((SN*(1-1)+1:SN*(1-1)+SN),:)*b{l}(q_k+2+1:b_n-SN)+co2pc((SN*(Rr-1)+1:SN*(Rr-1)+SN)).*b{l}(b_n-SN+1:b_n)];
                    end
                    if YF>1
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+LOGtimetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(q_k+2)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(q_k+2+1:b_n-SN)+y_hat{c,l}(:,YF-1).*b{l}(b_n-SN+1:b_n)];
                    end
                elseif m>(M-Spl)
                    if YF==1
                        income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(q_k+2+1:b_n-SN);
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+LOGtimetrend(SN*(Rr+1-1)+1:SN*(Rr+1-1)+SN).*b{l}(q_k+2)+Y{m,c}((SN*(1-1)+1:SN*(1-1)+SN),:).*income_b+co2pc((SN*(Rr-1)+1:SN*(Rr-1)+SN)).*b{l}(b_n-SN+1:b_n)];
                    end
                    if YF>1
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+LOGtimetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(q_k+2)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b+y_hat{c,l}(:,YF-1).*b{l}(b_n-SN+1:b_n)];
                    end
                end
              elseif unc==2
               if m<=(M-Spl)
                   
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+LOGtimetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(q_k+2)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(q_k+2+1:b_n-SN)+co2pc((SN*(Rr-1-theta+YF)+1:SN*(Rr-1-theta+YF)+SN)).*b{l}(b_n-SN+1:b_n)];
                    
                elseif m>(M-Spl)
                   
                        income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(q_k+2+1:b_n-SN);
                     
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+LOGtimetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(q_k+2)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b+co2pc((SN*(Rr-1-theta+YF)+1:SN*(Rr-1-theta+YF)+SN)).*b{l}(b_n-SN+1:b_n)];
                   
                end
              end
            end
         end
               if logs==2 | logs==4
               y_hat{c,l}=exp(y_hat{c,l}+(e'*e)/(n-k)/2);
               end
    end
end
%---------------------------------
                 
                        if logs==1 | logs==3 
                            f_hat(r,l)=(ones(SN,1)'*[([y_m]-y_hat_m).^2]);
                            f_hatA(r,l)=(ones(SN,1)'*[([y_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))]-y_hat_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))).^2]);
                            
                        elseif logs==2 | logs==4
                            f_hat(r,l)=(ones(SN,1)'*[(exp(y_m)-exp(y_hat_m+(res'*res)/(nu-ka)/2)).^2]);
                            f_hatA(r,l)=(ones(SN,1)'*[(exp(y_m).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))-exp(y_hat_m+(res'*res)/(nu-ka)/2).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))).^2]);
                            
                        end;
                        clear X1 x_time btime y_m b_n res nu ka 
                    end;
                    
                end;
            end;
            
% 13th loop INCLUDING State fixed effects  and CRISIS DUMMYS   
             %waitbar(l/w)
             for m=1:MM
                 l=l+1;
                 for r=1:N
                     Laufzeit=Laufzeit-1;
                     clc
                     disp(['Regressions left: ',num2str(Laufzeit)])

                     if R+r>=31  %since we can only take crisis3 in when including at least year 1990 (dummy is for 90-91)
                         if lag==1
                             if m<=(M-Spl)
                                 X1=[state(1:SN*(R+r),:) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) crisis3(1:SN*(R+r))  X{m}(1:SN*(R+r),:)];
                             elseif m>(M-Spl)
                                 X1=[state(1:SN*(R+r),:) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) crisis3(1:SN*(R+r)) incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag}];
                             end
                         elseif lag==2
                             if m<=(M-Spl)
                                 X1=[state(1:SN*(R+r),:) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) crisis3(1:SN*(R+r))  X{m}(1:SN*(R+r),:) LAGco2pc(1:SN*(R+r),:)];
                             elseif m>(M-Spl)
                                 X1=[state(1:SN*(R+r),:) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) crisis3(1:SN*(R+r))  incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(R+r),:)];
                             end
                         end
                     elseif R+r<31
                         if lag==1
                             if m<=(M-Spl)
                                 X1=[state(1:SN*(R+r),:) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r))  X{m}(1:SN*(R+r),:)];
                             elseif m>(M-Spl)
                                 X1=[state(1:SN*(R+r),:) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r))   incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag}];
                             end
                         elseif lag==2
                             if m<=(M-Spl)
                                 X1=[state(1:SN*(R+r),:) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r))  X{m}(1:SN*(R+r),:) LAGco2pc(1:SN*(R+r),:)];
                             elseif m>(M-Spl)
                                 X1=[state(1:SN*(R+r),:) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r))  incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(R+r),:)];
                             end
                         end

                     end
                     [nu,ka]=size(X1);
                        [b{l},a,res]=regress(co2pc(1:SN*(R+r),:),X1);
                     b_n=size(b{l},1);
                     if R+r>=31
                         b{l}=b{l}([1:SN,(SN+4):b_n]);
                     elseif R+r<31
                         b{l}=b{l}([1:SN,(SN+3):b_n])  ;
                     end
                     clear b_n;

                     
                     b_n=size(b{l},1);

                     y_m=(co2pc(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN));
                     if lag==1
                         if m<=(M-Spl)
                             y_hat_m=[b{l}(1:SN)+Z{m}((SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN),:)*b{l}(SN+1:b_n)];
                         elseif m>(M-Spl)
                         income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(SN+1:b_n);
                         y_hat_m=[b{l}(1:SN)+X{m}((SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN),:).*income_b];
                         end
                     elseif lag==2
                         if unc==1
                         if m<=(M-Spl)
                             y_hat_m=[b{l}(1:SN)+Z{m}((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(SN+1:b_n-SN)+LAGco2pc((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                                 for t=2:theta
                                 y_hat_m=[b{l}(1:SN)+Z{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(SN+1:b_n-SN)+y_hat_m.*b{l}(b_n-SN+1:b_n)];
                                 end
                         elseif m>(M-Spl)
                             income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(SN+1:b_n-SN);
                             y_hat_m=[b{l}(1:SN)+X{m}((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:).*income_b+LAGco2pc((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                             for t=2:theta
                                 y_hat_m=[b{l}(1:SN)+X{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:).*income_b+y_hat_m.*b{l}(b_n-SN+1:b_n)];
                             end
                         end
                         elseif unc==2
                                                  if m<=(M-Spl)
                             t=theta;
                                 y_hat_m=[b{l}(1:SN)+Z{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(SN+1:b_n-SN)+LAGco2pc((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                                 
                         elseif m>(M-Spl)
                             income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(SN+1:b_n-SN);
                             t=theta;
                                 y_hat_m=[b{l}(1:SN)+X{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:).*income_b+LAGco2pc((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                             
                         end
                         end
                     end
                     %----------------------------------forecasts for each model from Rr+1 bis YEARS+YEARSFOR
                     if  r==N
                         if lag==1
                             if m<=(M-Spl)
                                 X1=[state(1:SN*(Rr),:) crisis1(1:SN*(Rr)) crisis2(1:SN*(Rr)) crisis3(1:SN*(Rr))  X{m}(1:SN*(Rr),:)];
                             elseif m>(M-Spl)
                                 X1=[state(1:SN*(Rr),:) crisis1(1:SN*(Rr)) crisis2(1:SN*(Rr)) crisis3(1:SN*(Rr)) incomespline_{N+1,(m-(M-Spl)+(10-Spl)),logs,lag}];
                             end
                         elseif lag==2
                             if m<=(M-Spl)
                                 X1=[state(1:SN*(Rr),:) crisis1(1:SN*(Rr)) crisis2(1:SN*(Rr)) crisis3(1:SN*(Rr))  X{m}(1:SN*(Rr),:) LAGco2pc(1:SN*(Rr),:)];
                             elseif m>(M-Spl)
                                 X1=[state(1:SN*(Rr),:) crisis1(1:SN*(Rr)) crisis2(1:SN*(Rr)) crisis3(1:SN*(Rr))  incomespline_{N+1,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(Rr),:)];
                             end
                         end

              clear e n k
            [b{l},a,e,int,stat]=regress(co2pc(1:SN*(Rr),:),X1);
            [n,k]=size(X1);
            %SIC
            SIC(1,l)=[log((e'*e)/(n))+((k/n)*log(n))];
            %AIC
            AIC(1,l)=[log((e'*e)/(n))+(2*k/n)];
            %simple R2
            R_2(1,l)=stat(1,1);
            % adjusted R2
            R2adj(1,l)=1-((n-1)/(n-k))*(1-R_2(1,l));
                         b_n=size(b{l},1);

                         b{l}=b{l}([1:SN,(SN+4):b_n]);

                         clear b_n;

                         
                         b_n=size(b{l},1);


                         for c=Cc:CC
                             for YF=1:YEARSFOR;
                                 if lag==1
                                     if m<=(M-Spl)

                                         y_hat{c,l}(:,YF)=[b{l}(1:SN)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(SN+1:b_n)];
                                     elseif m>(M-Spl)
                                         income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(SN+1:b_n);
                                         y_hat{c,l}(:,YF)=[b{l}(1:SN)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b];
                                     end
                                 elseif lag==2 
                                     if unc==1
                                     if m<=(M-Spl)
                                         if YF==1
                                             y_hat{c,l}(:,YF)=[b{l}(1:SN)+Y{m,c}((SN*(1-1)+1:SN*(1-1)+SN),:)*b{l}(SN+1:b_n-SN)+co2pc((SN*(Rr-1)+1:SN*(Rr-1)+SN)).*b{l}(b_n-SN+1:b_n)];
                                         end
                                         if YF>1
                                             y_hat{c,l}(:,YF)=[b{l}(1:SN)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(SN+1:b_n-SN)+y_hat{c,l}(:,YF-1).*b{l}(b_n-SN+1:b_n)];
                                         end
                                     elseif m>(M-Spl)
                                         if YF==1
                                             income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(SN+1:b_n-SN);
                                             y_hat{c,l}(:,YF)=[b{l}(1:SN)+Y{m,c}((SN*(1-1)+1:SN*(1-1)+SN),:).*income_b+co2pc((SN*(Rr-1)+1:SN*(Rr-1)+SN)).*b{l}(b_n-SN+1:b_n)];
                                         end
                                         if YF>1
                                             y_hat{c,l}(:,YF)=[b{l}(1:SN)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b+y_hat{c,l}(:,YF-1).*b{l}(b_n-SN+1:b_n)];
                                         end
                                     end
                                     elseif unc==2
                                     if m<=(M-Spl)
                                      
                                             y_hat{c,l}(:,YF)=[b{l}(1:SN)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(SN+1:b_n-SN)+co2pc((SN*(Rr-1-theta+YF)+1:SN*(Rr-1-theta+YF)+SN)).*b{l}(b_n-SN+1:b_n)];
                                         
                                     elseif m>(M-Spl)
                                         
                                             income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(SN+1:b_n-SN);
                                         
                                             y_hat{c,l}(:,YF)=[b{l}(1:SN)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b+co2pc((SN*(Rr-1-theta+YF)+1:SN*(Rr-1-theta+YF)+SN)).*b{l}(b_n-SN+1:b_n)];
                                         
                                     end
                                     end
                                 end
                              end
               if logs==2 | logs==4
               y_hat{c,l}=exp(y_hat{c,l}+(e'*e)/(n-k)/2);
               end
                         end
                     end
                     %---------------------------------
                     
                     if logs==1 | logs==3
                         f_hat(r,l)=(ones(SN,1)'*[([y_m]-y_hat_m).^2]);
                         f_hatA(r,l)=(ones(SN,1)'*[([y_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))]-y_hat_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))).^2]);
                         
                     elseif logs==2  | logs==4
                         f_hat(r,l)=(ones(SN,1)'*[(exp(y_m)-exp(y_hat_m+(res'*res)/(nu-ka)/2)).^2]);
                         f_hatA(r,l)=(ones(SN,1)'*[(exp(y_m).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))-exp(y_hat_m+(res'*res)/(nu-ka)/2).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))).^2]);
                         
                     end;
                     clear X1 x_time btime y_m b_n y_hat_m res nu ka 
                 end;

             end;

            
% 14th loop INCLUDING CRISIS DUMMYS      NO STATE FIXED EFFECTS 
%INCLUDING US AGG REGRESSIONS 
            for m=1:MM 
                 l=l+1;
                for r=1:N
                    Laufzeit=Laufzeit-1;
                    clc
                    disp(['Regressions left: ',num2str(Laufzeit)])
                    
                  if Rr-(R+r)<=11
                    if lag==1
                        if m<=(M-Spl)
                        X1=[ones(SN*(R+r),1) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) crisis3(1:SN*(R+r)) X{m}(1:SN*(R+r),:)];
                                         X1A=[ones(1*(R+r),1) crisis1A(1:1*(R+r)) crisis2A(1:1*(R+r)) crisis3A(1:1*(R+r)) XA{m}(1:1*(R+r),:)];
                        elseif m>(M-Spl)
                                 X1=[ones(SN*(R+r),1) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) crisis3(1:SN*(R+r)) incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag}];
                                end
                    elseif lag==2
                        if m<=(M-Spl)
                        X1=[ones(SN*(R+r),1) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) crisis3(1:SN*(R+r)) X{m}(1:SN*(R+r),:) LAGco2pc(1:SN*(R+r),:)]; 
                                             X1A=[ones(1*(R+r),1) crisis1A(1:1*(R+r)) crisis2A(1:1*(R+r)) crisis3A(1:1*(R+r)) XA{m}(1:1*(R+r),:) LAGco2pcA(1:1*(R+r),:)]; 
                        elseif m>(M-Spl)
                         X1=[ones(SN*(R+r),1) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) crisis3(1:SN*(R+r))  incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(R+r),:)];    
                        end
                    end
                    
                  elseif Rr-(R+r)>11
                      
                       if lag==1
                        if m<=(M-Spl)
                        X1=[ones(SN*(R+r),1) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r))  X{m}(1:SN*(R+r),:)];
                                         X1A=[ones(1*(R+r),1) crisis1A(1:1*(R+r)) crisis2A(1:1*(R+r))  XA{m}(1:1*(R+r),:)];
                        elseif m>(M-Spl)
                                 X1=[ones(SN*(R+r),1) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r))  incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag}];
                                end
                    elseif lag==2
                        if m<=(M-Spl)
                        X1=[ones(SN*(R+r),1) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r))   X{m}(1:SN*(R+r),:) LAGco2pc(1:SN*(R+r),:)]; 
                                         X1A=[ones(1*(R+r),1) crisis1A(1:1*(R+r)) crisis2A(1:1*(R+r))   XA{m}(1:1*(R+r),:) LAGco2pcA(1:1*(R+r),:)]; 
                        elseif m>(M-Spl)
                         X1=[ones(SN*(R+r),1) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r))  incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(R+r),:)];    
                        end
                    end
                end
                    [nu,ka]=size(X1);
                        [b{l},a,res]=regress(co2pc(1:SN*(R+r),:),X1);
                    
                     b_n=size(b{l},1);
                    if Rr-(R+r)<=11
                      b{l}=b{l}([1,(5):b_n]);
                    elseif Rr-(R+r)>11
                      b{l}=b{l}([1,(4):b_n])  ;
                    end
                    clear b_n;
                            
                    b_n=size(b{l},1);
                    
                    y_m=(co2pc(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN));
                    if m<=(M-Spl)
                                            [nuA,kaA]=size(X1A);
                                                [bA{l},aA,resA]=regress(co2pcA(1:1*(R+r),:),X1A);

                                             b_nA=size(bA{l},1);
                                            if Rr-(R+r)<=11
                                              bA{l}=bA{l}([1,(5):b_nA]);
                                            elseif Rr-(R+r)>11
                                              bA{l}=bA{l}([1,(4):b_nA])  ;
                                            end
                                            clear b_nA;
                                                      
                                            b_nA=size(bA{l},1);

                                            y_mA=(co2pcA(1*(R+r+theta-1)+1:1*(R+r+theta-1)+1));
                    end
                    if lag==1
                        if m<=(M-Spl)
                        y_hat_m=[ones(SN,1).*b{l}(1)+Z{m}((SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN),:)*b{l}(1+1:b_n)];
                                             y_hat_mA=[ones(1,1).*bA{l}(1)+ZA{m}((1*(R+r+theta-1)+1:1*(R+r+theta-1)+1),:)*bA{l}(1+1:b_nA)];
                        elseif m>(M-Spl)
                                income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(1+1:b_n);
                              y_hat_m=[ones(SN,1).*b{l}(1)+X{m}((SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN),:).*income_b];
                                end 
                    elseif lag==2
                        if unc==1
                        if m<=(M-Spl)
                        y_hat_m=[ones(SN,1).*b{l}(1)+Z{m}((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(1+1:b_n-SN)+LAGco2pc((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                        for t=2:theta
                        y_hat_m=[ones(SN,1).*b{l}(1)+Z{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(1+1:b_n-SN)+y_hat_m.*b{l}(b_n-SN+1:b_n)];
                        end      
                                             y_hat_mA=[ones(1,1).*bA{l}(1)+ZA{m}((1*(R+r+1-1)+1:1*(R+r+1-1)+1),:)*bA{l}(1+1:b_nA-1)+LAGco2pcA((1*(R+r+1-1)+1:1*(R+r+1-1)+1),:)*bA{l}(b_nA-1+1:b_nA)];
                                            for t=2:theta
                                            y_hat_mA=[ones(1,1).*bA{l}(1)+ZA{m}((1*(R+r+t-1)+1:1*(R+r+t-1)+1),:)*bA{l}(1+1:b_nA-1)+y_hat_mA.*bA{l}(b_nA-1+1:b_nA)];
                                            end      
                         elseif m>(M-Spl)
                                income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(1+1:b_n-SN);
                         y_hat_m=[ones(SN,1).*b{l}(1)+X{m}((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:).*income_b+LAGco2pc((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                        for t=2:theta
                        y_hat_m=[ones(SN,1).*b{l}(1)+X{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:).*income_b+y_hat_m.*b{l}(b_n-SN+1:b_n)];
                        end
                        end
                        elseif unc==2
                                                if m<=(M-Spl)
                       t=theta;
                        y_hat_m=[ones(SN,1).*b{l}(1)+Z{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(1+1:b_n-SN)+LAGco2pc((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                              
                                             t=theta;
                                            y_hat_mA=[ones(1,1).*bA{l}(1)+ZA{m}((1*(R+r+t-1)+1:1*(R+r+t-1)+1),:)*bA{l}(1+1:b_nA-1)+LAGco2pcA((1*(R+r+t-1)+1:1*(R+r+t-1)+1),:)*bA{l}(b_nA-1+1:b_nA)];
                                                  
                         elseif m>(M-Spl)
                                income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(1+1:b_n-SN);
                        t=theta;
                        y_hat_m=[ones(SN,1).*b{l}(1)+X{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:).*income_b+LAGco2pc((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                        
                    end
                        end
                        
                    end
%----------------------------------forecasts for each model from Rr+1 bis YEARS+YEARSFOR           
if  r==N

    if lag==1
        if m<=(M-Spl)
            X1=[ones(SN*(Rr),1) crisis1(1:SN*(Rr)) crisis2(1:SN*(Rr)) crisis3(1:SN*(Rr)) X{m}(1:SN*(Rr),:)];
        elseif m>(M-Spl)
            X1=[ones(SN*(Rr),1) crisis1(1:SN*(Rr)) crisis2(1:SN*(Rr)) crisis3(1:SN*(Rr)) incomespline_{N+1,(m-(M-Spl)+(10-Spl)),logs,lag}];
        end
    elseif lag==2
        if m<=(M-Spl)
            X1=[ones(SN*(Rr),1) crisis1(1:SN*(Rr)) crisis2(1:SN*(Rr)) crisis3(1:SN*(Rr)) X{m}(1:SN*(Rr),:) LAGco2pc(1:SN*(Rr),:)];
        elseif m>(M-Spl)
            X1=[ones(SN*(Rr),1) crisis1(1:SN*(Rr)) crisis2(1:SN*(Rr)) crisis3(1:SN*(Rr))  incomespline_{N+1,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(Rr),:)];
        end
    end

clear e n k
            [b{l},a,e,int,stat]=regress(co2pc(1:SN*(Rr),:),X1);
            [n,k]=size(X1);
            %SIC
            SIC(1,l)=[log((e'*e)/(n))+((k/n)*log(n))];
            %AIC
            AIC(1,l)=[log((e'*e)/(n))+(2*k/n)];
            %simple R2
            R_2(1,l)=stat(1,1);
            % adjusted R2
            R2adj(1,l)=1-((n-1)/(n-k))*(1-R_2(1,l));

    b_n=size(b{l},1);

    b{l}=b{l}([1,(5):b_n]);

    clear b_n;
    
    b_n=size(b{l},1);

    for c=Cc:CC
        for YF=1:YEARSFOR;
            if lag==1
                if m<=(M-Spl)
                    y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(1+1:b_n)];
                elseif m>(M-Spl)
                    income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(1+1:b_n);
                    y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b];
                end
            elseif lag==2 
                if unc==1
                if m<=(M-Spl)
                    if YF==1
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+Y{m,c}((SN*(1-1)+1:SN*(1-1)+SN),:)*b{l}(1+1:b_n-SN)+co2pc((SN*(Rr-1)+1:SN*(Rr-1)+SN)).*b{l}(b_n-SN+1:b_n)];
                    end
                    if YF>1
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(1+1:b_n-SN)+y_hat{c,l}(:,YF-1).*b{l}(b_n-SN+1:b_n)];
                    end
                elseif m>(M-Spl)
                    if YF==1
                        income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(1+1:b_n-SN);
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+Y{m,c}((SN*(1-1)+1:SN*(1-1)+SN),:).*income_b+co2pc((SN*(Rr-1)+1:SN*(Rr-1)+SN)).*b{l}(b_n-SN+1:b_n)];
                    end
                    if YF>1
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b+y_hat{c,l}(:,YF-1).*b{l}(b_n-SN+1:b_n)];
                    end
                end
                elseif unc==2
                 if m<=(M-Spl)
                   
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(1+1:b_n-SN)+co2pc((SN*(Rr-1-theta+YF)+1:SN*(Rr-1-theta+YF)+SN)).*b{l}(b_n-SN+1:b_n)];
                   
                elseif m>(M-Spl)
                    
                        income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(1+1:b_n-SN);
                      
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b+co2pc((SN*(Rr-1-theta+YF)+1:SN*(Rr-1-theta+YF)+SN)).*b{l}(b_n-SN+1:b_n)];
                   
                end
                end
            end
         end
               if logs==2 | logs==4
               y_hat{c,l}=exp(y_hat{c,l}+(e'*e)/(n-k)/2);
               end
    end
end
%---------------------------------
                    
                    if logs==1 | logs==3 
                        f_hat(r,l)=(ones(SN,1)'*[([y_m]-y_hat_m).^2]);
                        f_hatA(r,l)=(ones(SN,1)'*[([y_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))]-y_hat_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))).^2]);
                       
                       if m<=(M-Spl) 
                       f_hatagg(r,l)=[(((ones(SN,1)'*[y_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))])/popA(1*(R+r+theta-1)+1:1*(R+r+theta)))-((ones(SN,1)'*(y_hat_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))))/popA(1*(R+r+theta-1)+1:1*(R+r+theta)))).^2];
                        f_hatAagg(r,l)=([(ones(SN,1)'*[y_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))]-(ones(SN,1)'*(y_hat_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))))).^2]);
                        
                        f_hataggA(r,l)=([([y_mA]-y_hat_mA).^2]);
                        f_hatAaggA(r,l)=([([y_mA.*popA(1*(R+r+theta-1)+1:1*(R+r+theta))]-y_hat_mA.*popA(1*(R+r+theta-1)+1:1*(R+r+theta))).^2]);
                       end
                       elseif logs==2  | logs==4
                        f_hat(r,l)=(ones(SN,1)'*[(exp(y_m)-exp(y_hat_m+(res'*res)/(nu-ka)/2)).^2]);
                        f_hatA(r,l)=(ones(SN,1)'*[(exp(y_m).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))-exp(y_hat_m+(res'*res)/(nu-ka)/2).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))).^2]);
                        
                        if m<=(M-Spl)
                        f_hatagg(r,l)=[(((ones(SN,1)'*[exp(y_m).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))])/popA(1*(R+r+theta-1)+1:1*(R+r+theta)))-((ones(SN,1)'*(exp(y_hat_m+(res'*res)/(nu-ka)/2).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))))/popA(1*(R+r+theta-1)+1:1*(R+r+theta)))).^2];
                        f_hatAagg(r,l)=([(ones(SN,1)'*[exp(y_m).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))]-(ones(SN,1)'*(exp(y_hat_m+(res'*res)/(nu-ka)/2).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))))).^2]);
                        
                        f_hataggA(r,l)=([([exp(y_mA)]-exp(y_hat_mA+(resA'*resA)/(nuA-kaA)/2)).^2]);
                        f_hatAaggA(r,l)=([([exp(y_mA).*popA(1*(R+r+theta-1)+1:1*(R+r+theta))]-exp(y_hat_mA+(resA'*resA)/(nuA-kaA)/2).*popA(1*(R+r+theta-1)+1:1*(R+r+theta))).^2]);
                        end
                        end;
                    clear X1 x_time btime y_m b_n res nu ka y_hat_m y_hat_mA y_mA b_nA resA nuA kaA 
                end;
                
            end;
% 15th loop INCLUDING CRISIS DUMMYS      NO STATE FIXED EFFECTS---BUT QUALITY DUMMIES 
            for q=1:QDN    
                 %waitbar(l/w)
                for m=1:MM 
                     l=l+1;
                    for r=1:N
                        Laufzeit=Laufzeit-1;
                        clc
                        disp(['Regressions left: ',num2str(Laufzeit)])
                        
                      if R+r>=31  
                        if lag==1
                            if m<=(M-Spl)
                            X1=[ones(SN*(R+r),1)  crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) crisis3(1:SN*(R+r)) QD{q}(1:SN*(R+r),:) X{m}(1:SN*(R+r),:)];
                            elseif m>(M-Spl)
                                 X1=[ones(SN*(R+r),1)  crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) crisis3(1:SN*(R+r)) QD{q}(1:SN*(R+r),:)  incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag}];
                                end
                        elseif lag==2
                            if m<=(M-Spl)
                            X1=[ones(SN*(R+r),1)  crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) crisis3(1:SN*(R+r)) QD{q}(1:SN*(R+r),:)  X{m}(1:SN*(R+r),:) LAGco2pc(1:SN*(R+r),:)];
                            elseif m>(M-Spl)
                            X1=[ones(SN*(R+r),1)  crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) crisis3(1:SN*(R+r)) QD{q}(1:SN*(R+r),:)  incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(R+r),:)];    
                            end
                        end
                        
                     elseif R+r<31
                         
                           if lag==1
                            if m<=(M-Spl)
                            X1=[ones(SN*(R+r),1)  crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r))  QD{q}(1:SN*(R+r),:)  X{m}(1:SN*(R+r),:)];
                            elseif m>(M-Spl)
                                 X1=[ones(SN*(R+r),1)  crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r))  QD{q}(1:SN*(R+r),:) incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag}];
                                end
                        elseif lag==2
                            if m<=(M-Spl)
                            X1=[ones(SN*(R+r),1) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r))  QD{q}(1:SN*(R+r),:) X{m}(1:SN*(R+r),:) LAGco2pc(1:SN*(R+r),:)];
                            elseif m>(M-Spl)
                            X1=[ones(SN*(R+r),1)  crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) QD{q}(1:SN*(R+r),:)  incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(R+r),:)];    
                            end
                        end
                    end
                        [nu,ka]=size(X1);
                        [b{l},a,res]=regress(co2pc(1:SN*(R+r),:),X1);
                        
                     b_n=size(b{l},1);
                    if R+r>=31
                      b{l}=b{l}([1,(5):b_n]);
                    elseif R+r<31
                      b{l}=b{l}([1,(4):b_n])  ;
                    end
                    clear b_n;
                  
                        b_n=size(b{l},1);
                        q_k=size(QD{q},2);
                        
                        y_m=(co2pc(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN));
                        if lag==1
                            if m<=(M-Spl)
                            y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+Z{m}((SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN),:)*b{l}(q_k+2:b_n)];
                            elseif m>(M-Spl)
                                income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(q_k+2:b_n);
                              y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+X{m}((SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN),:).*income_b];
                                end 
                        elseif lag==2
                            if unc==1
                            if m<=(M-Spl)
                            y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+Z{m}((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(q_k+2:b_n-SN)+LAGco2pc((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                            for t=2:theta
                            y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+Z{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(q_k+2:b_n-SN)+y_hat_m.*b{l}(b_n-SN+1:b_n)];
                            end     
                             elseif m>(M-Spl)
                                income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(q_k+2:b_n-SN);
                            y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+X{m}((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:).*income_b+LAGco2pc((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                            for t=2:theta
                            y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+X{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:).*income_b+y_hat_m.*b{l}(b_n-SN+1:b_n)];
                            end  
                            end
                            elseif unc==2
                                                        if m<=(M-Spl)
                            t=theta;
                            y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+Z{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(q_k+2:b_n-SN)+LAGco2pc((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                                 
                             elseif m>(M-Spl)
                                income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(q_k+2:b_n-SN);
                            t=theta;
                            y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+X{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:).*income_b+LAGco2pc((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                              
                        end
                            end
                            
                        end
%----------------------------------forecasts for each model from Rr+1 bis YEARS+YEARSFOR           
if  r==N

    if lag==1
        if m<=(M-Spl)
            X1=[ones(SN*(Rr),1)  crisis1(1:SN*(Rr)) crisis2(1:SN*(Rr)) crisis3(1:SN*(Rr)) QD{q}(1:SN*(Rr),:) X{m}(1:SN*(Rr),:)];
        elseif m>(M-Spl)
            X1=[ones(SN*(Rr),1)  crisis1(1:SN*(Rr)) crisis2(1:SN*(Rr)) crisis3(1:SN*(Rr)) QD{q}(1:SN*(Rr),:)  incomespline_{N+1,(m-(M-Spl)+(10-Spl)),logs,lag}];
        end
    elseif lag==2
        if m<=(M-Spl)
            X1=[ones(SN*(Rr),1)  crisis1(1:SN*(Rr)) crisis2(1:SN*(Rr)) crisis3(1:SN*(Rr)) QD{q}(1:SN*(Rr),:)  X{m}(1:SN*(Rr),:) LAGco2pc(1:SN*(Rr),:)];
        elseif m>(M-Spl)
            X1=[ones(SN*(Rr),1)  crisis1(1:SN*(Rr)) crisis2(1:SN*(Rr)) crisis3(1:SN*(Rr)) QD{q}(1:SN*(Rr),:)  incomespline_{N+1,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(Rr),:)];
        end
    end

clear e n k
            [b{l},a,e,int,stat]=regress(co2pc(1:SN*(Rr),:),X1);
            [n,k]=size(X1);
            %SIC
            SIC(1,l)=[log((e'*e)/(n))+((k/n)*log(n))];
            %AIC
            AIC(1,l)=[log((e'*e)/(n))+(2*k/n)];
            %simple R2
            R_2(1,l)=stat(1,1);
            % adjusted R2
            R2adj(1,l)=1-((n-1)/(n-k))*(1-R_2(1,l));

    b_n=size(b{l},1);

    b{l}=b{l}([1,(5):b_n]);

    clear b_n;
   
    b_n=size(b{l},1);
    q_k=size(QD{q},2);

    for c=Cc:CC
        for YF=1:YEARSFOR;
            if lag==1
                if m<=(M-Spl)
                    y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(q_k+2:b_n)];
                elseif m>(M-Spl)
                    income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(q_k+2:b_n);
                    y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b];
                end
            elseif lag==2 
                if unc==1
                if m<=(M-Spl)
                    if YF==1
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+Y{m,c}((SN*(1-1)+1:SN*(1-1)+SN),:)*b{l}(q_k+2:b_n-SN)+co2pc((SN*(Rr-1)+1:SN*(Rr-1)+SN)).*b{l}(b_n-SN+1:b_n)];
                    end
                    if YF>1
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(q_k+2:b_n-SN)+y_hat{c,l}(:,YF-1).*b{l}(b_n-SN+1:b_n)];
                    end
                elseif m>(M-Spl)
                    if YF==1
                        income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(q_k+2:b_n-SN);
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+Y{m,c}((SN*(1-1)+1:SN*(1-1)+SN),:).*income_b+co2pc((SN*(Rr-1)+1:SN*(Rr-1)+SN)).*b{l}(b_n-SN+1:b_n)];
                    end
                    if YF>1
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b+y_hat{c,l}(:,YF-1).*b{l}(b_n-SN+1:b_n)];
                    end
                end
                elseif unc==2
                 if m<=(M-Spl)
                    
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(q_k+2:b_n-SN)+co2pc((SN*(Rr-1-theta+YF)+1:SN*(Rr-1-theta+YF)+SN)).*b{l}(b_n-SN+1:b_n)];
                    
                elseif m>(M-Spl)
                    
                        income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(q_k+2:b_n-SN);
                     
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b+co2pc((SN*(Rr-1-theta+YF)+1:SN*(Rr-1-theta+YF)+SN)).*b{l}(b_n-SN+1:b_n)];
                    
                end
                end
            end
         end
               if logs==2 | logs==4
               y_hat{c,l}=exp(y_hat{c,l}+(e'*e)/(n-k)/2);
               end
    end
end
%---------------------------------
                 
                        if logs==1 | logs==3 
                            f_hat(r,l)=(ones(SN,1)'*[([y_m]-y_hat_m).^2]);
                            f_hatA(r,l)=(ones(SN,1)'*[([y_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))]-y_hat_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))).^2]);
                            
                        elseif logs==2 | logs==4
                            f_hat(r,l)=(ones(SN,1)'*[(exp(y_m)-exp(y_hat_m+(res'*res)/(nu-ka)/2)).^2]);
                            f_hatA(r,l)=(ones(SN,1)'*[(exp(y_m).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))-exp(y_hat_m+(res'*res)/(nu-ka)/2).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))).^2]);
                            
                        end;
                        clear X1 x_time btime y_m b_n res nu ka 
                    end;
                    
                end;
            end;           
            
 % 16th loop INCLUDING State fixed effects  and CRISIS DUMMYS and LOG-TIMETREND
             %waitbar(l/w)
            for m=1:MM    
                 l=l+1;
                for r=1:N
                    Laufzeit=Laufzeit-1;
                    clc
                    disp(['Regressions left: ',num2str(Laufzeit)])
                    
                  if R+r>=32  %since we can only take crisis3 in when including at least year 1990 (dummy is for 90-91)  
                    if lag==1
                        if m<=(M-Spl)
                        X1=[state(1:SN*(R+r),:) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) crisis3(1:SN*(R+r)) LOGtimetrend(1:SN*(R+r)) X{m}(1:SN*(R+r),:)];
                        elseif m>(M-Spl)
                                 X1=[state(1:SN*(R+r),:) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) crisis3(1:SN*(R+r)) LOGtimetrend(1:SN*(R+r)) incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag}];
                                end
                    elseif lag==2
                        if m<=(M-Spl)
                        X1=[state(1:SN*(R+r),:) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) crisis3(1:SN*(R+r)) LOGtimetrend(1:SN*(R+r)) X{m}(1:SN*(R+r),:) LAGco2pc(1:SN*(R+r),:)]; 
                        elseif m>(M-Spl)
                         X1=[state(1:SN*(R+r),:) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) crisis3(1:SN*(R+r)) LOGtimetrend(1:SN*(R+r)) incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(R+r),:)];     
                        end
                   end
                elseif R+r<32
                    if lag==1
                        if m<=(M-Spl)
                        X1=[state(1:SN*(R+r),:) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r))  LOGtimetrend(1:SN*(R+r)) X{m}(1:SN*(R+r),:)];
                        elseif m>(M-Spl)
                                 X1=[state(1:SN*(R+r),:) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r))  LOGtimetrend(1:SN*(R+r)) incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag}];
                                end
                    elseif lag==2
                        if m<=(M-Spl)
                        X1=[state(1:SN*(R+r),:) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r))  LOGtimetrend(1:SN*(R+r)) X{m}(1:SN*(R+r),:) LAGco2pc(1:SN*(R+r),:)]; 
                        elseif m>(M-Spl)
                         X1=[state(1:SN*(R+r),:) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) LOGtimetrend(1:SN*(R+r)) incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(R+r),:)];     
                        end
                   end

                end
                    [nu,ka]=size(X1);
                        [b{l},a,res]=regress(co2pc(1:SN*(R+r),:),X1);
                     b_n=size(b{l},1);
                    if R+r>=32
                      b{l}=b{l}([1:SN,(SN+4):b_n]);
                    elseif R+r<32
                      b{l}=b{l}([1:SN,(SN+3):b_n])  ;
                    end
                    clear b_n;
                    
                             
                    b_n=size(b{l},1);
                    
                    y_m=(co2pc(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN));
                    if lag==1
                        if m<=(M-Spl)
                        y_hat_m=[eye(SN,SN)*b{l}(1:SN)+LOGtimetrend(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN).*b{l}(SN+1)+Z{m}((SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN),:)*b{l}(SN+1+1:b_n)];
                        elseif m>(M-Spl)
                                income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(SN+1+1:b_n);
                              y_hat_m=[eye(SN,SN)*b{l}(1:SN)+LOGtimetrend(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN).*b{l}(SN+1)+X{m}((SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN),:).*income_b];
                                end 
                    elseif lag==2
                        if unc==1
                        if m<=(M-Spl)
                        y_hat_m=[eye(SN,SN)*b{l}(1:SN)+LOGtimetrend(SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN).*b{l}(SN+1)+Z{m}((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(SN+1+1:b_n-SN)+LAGco2pc((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                        for t=2:theta
                        y_hat_m=[eye(SN,SN)*b{l}(1:SN)+LOGtimetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(SN+1)+Z{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(SN+1+1:b_n-SN)+y_hat_m.*b{l}(b_n-SN+1:b_n)];
                        end        
                         elseif m>(M-Spl)
                                income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(SN+1+1:b_n-SN);
                        y_hat_m=[eye(SN,SN)*b{l}(1:SN)+LOGtimetrend(SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN).*b{l}(SN+1)+X{m}((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:).*income_b+LAGco2pc((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                        for t=2:theta
                        y_hat_m=[eye(SN,SN)*b{l}(1:SN)+LOGtimetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(SN+1)+X{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:).*income_b+y_hat_m.*b{l}(b_n-SN+1:b_n)];
                        end  
                        end
                        elseif unc==2
                                                    if m<=(M-Spl)
                        t=theta;
                        y_hat_m=[eye(SN,SN)*b{l}(1:SN)+LOGtimetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(SN+1)+Z{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(SN+1+1:b_n-SN)+LAGco2pc((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                                
                         elseif m>(M-Spl)
                                income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(SN+1+1:b_n-SN);
                        t=theta;
                        y_hat_m=[eye(SN,SN)*b{l}(1:SN)+LOGtimetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(SN+1)+X{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:).*income_b+LAGco2pc((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                        end  
                    end
                    end
%----------------------------------forecasts for each model from Rr+1 bis YEARS+YEARSFOR           
if  r==N
    if lag==1
        if m<=(M-Spl)
            X1=[state(1:SN*(Rr),:) crisis1(1:SN*(Rr)) crisis2(1:SN*(Rr)) crisis3(1:SN*(Rr)) LOGtimetrend(1:SN*(Rr)) X{m}(1:SN*(Rr),:)];
        elseif m>(M-Spl)
            X1=[state(1:SN*(Rr),:) crisis1(1:SN*(Rr)) crisis2(1:SN*(Rr)) crisis3(1:SN*(Rr)) LOGtimetrend(1:SN*(Rr)) incomespline_{N+1,(m-(M-Spl)+(10-Spl)),logs,lag}];
        end
    elseif lag==2
        if m<=(M-Spl)
            X1=[state(1:SN*(Rr),:) crisis1(1:SN*(Rr)) crisis2(1:SN*(Rr)) crisis3(1:SN*(Rr)) LOGtimetrend(1:SN*(Rr)) X{m}(1:SN*(Rr),:) LAGco2pc(1:SN*(Rr),:)];
        elseif m>(M-Spl)
            X1=[state(1:SN*(Rr),:) crisis1(1:SN*(Rr)) crisis2(1:SN*(Rr)) crisis3(1:SN*(Rr)) LOGtimetrend(1:SN*(Rr)) incomespline_{N+1,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(Rr),:)];
        end
    end
clear e n k
            [b{l},a,e,int,stat]=regress(co2pc(1:SN*(Rr),:),X1);
            [n,k]=size(X1);
            %SIC
            SIC(1,l)=[log((e'*e)/(n))+((k/n)*log(n))];
            %AIC
            AIC(1,l)=[log((e'*e)/(n))+(2*k/n)];
            %simple R2
            R_2(1,l)=stat(1,1);
            % adjusted R2
            R2adj(1,l)=1-((n-1)/(n-k))*(1-R_2(1,l));
    b_n=size(b{l},1);

    b{l}=b{l}([1:SN,(SN+4):b_n]);

    clear b_n;

   
    b_n=size(b{l},1);

    for c=Cc:CC
        for YF=1:YEARSFOR;
            if lag==1
                if m<=(M-Spl)
                    y_hat{c,l}(:,YF)=[b{l}(1:SN)+LOGtimetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(SN+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(SN+1+1:b_n)];
                elseif m>(M-Spl)
                    income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(SN+1+1:b_n);
                    y_hat{c,l}(:,YF)=[b{l}(1:SN)+LOGtimetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(SN+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b];
                end
            elseif lag==2
              if unc==1
                if m<=(M-Spl)
                    if YF==1
                        y_hat{c,l}(:,YF)=[b{l}(1:SN)+LOGtimetrend(SN*(Rr+1-1)+1:SN*(Rr+1-1)+SN).*b{l}(SN+1)+Y{m,c}((SN*(1-1)+1:SN*(1-1)+SN),:)*b{l}(SN+1+1:b_n-SN)+co2pc((SN*(Rr-1)+1:SN*(Rr-1)+SN)).*b{l}(b_n-SN+1:b_n)];
                    end
                    if YF>1
                        y_hat{c,l}(:,YF)=[b{l}(1:SN)+LOGtimetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(SN+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(SN+1+1:b_n-SN)+y_hat{c,l}(:,YF-1).*b{l}(b_n-SN+1:b_n)];
                    end
                elseif m>(M-Spl)
                    if YF==1
                        income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(SN+1+1:b_n-SN);
                        y_hat{c,l}(:,YF)=[eye(SN,SN)*b{l}(1:SN)+LOGtimetrend(SN*(Rr+1-1)+1:SN*(Rr+1-1)+SN).*b{l}(SN+1)+Y{m,c}((SN*(1-1)+1:SN*(1-1)+SN),:).*income_b+co2pc((SN*(Rr-1)+1:SN*(Rr-1)+SN)).*b{l}(b_n-SN+1:b_n)];
                    end
                    if YF>1
                        y_hat{c,l}(:,YF)=[b{l}(1:SN)+LOGtimetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(SN+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b+y_hat{c,l}(:,YF-1).*b{l}(b_n-SN+1:b_n)];
                    end
                end
              elseif unc==2
              if m<=(M-Spl)
                    
                        y_hat{c,l}(:,YF)=[b{l}(1:SN)+LOGtimetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(SN+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(SN+1+1:b_n-SN)+co2pc((SN*(Rr-1-theta+YF)+1:SN*(Rr-1-theta+YF)+SN)).*b{l}(b_n-SN+1:b_n)];
                    
                elseif m>(M-Spl)
                    
                        income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(SN+1+1:b_n-SN);
                     
                        y_hat{c,l}(:,YF)=[b{l}(1:SN)+LOGtimetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(SN+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b+co2pc((SN*(Rr-1-theta+YF)+1:SN*(Rr-1-theta+YF)+SN)).*b{l}(b_n-SN+1:b_n)];
                    
                end
              end
            end
         end
               if logs==2 | logs==4
               y_hat{c,l}=exp(y_hat{c,l}+(e'*e)/(n-k)/2);
               end
    end
end
%---------------------------------
                    
                    if logs==1 | logs==3 
                        f_hat(r,l)=(ones(SN,1)'*[([y_m]-y_hat_m).^2]);
                        f_hatA(r,l)=(ones(SN,1)'*[([y_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))]-y_hat_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))).^2]);
                        
                    elseif logs==2  | logs==4
                        f_hat(r,l)=(ones(SN,1)'*[(exp(y_m)-exp(y_hat_m+(res'*res)/(nu-ka)/2)).^2]);
                        f_hatA(r,l)=(ones(SN,1)'*[(exp(y_m).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))-exp(y_hat_m+(res'*res)/(nu-ka)/2).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))).^2]);
                        
                    end;
                    clear X1 x_time btime y_m b_n y_hat_m res nu ka 
                end;
                
            end;
            
            
% 17th loop INCLUDING CRISIS DUMMYS and LOG-TIMETREND      NO STATE FIXED EFFECTS 
%INCLUDING US AGG REGRESSIONS 
            for m=1:MM 
                 l=l+1;
                for r=1:N
                    Laufzeit=Laufzeit-1;
                    clc
                    disp(['Regressions left: ',num2str(Laufzeit)])
                    
                  if Rr-(R+r)<=11
                    if lag==1
                        if m<=(M-Spl)
                        X1=[ones(SN*(R+r),1) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) crisis3(1:SN*(R+r)) LOGtimetrend(1:SN*(R+r)) X{m}(1:SN*(R+r),:)];
                                        X1A=[ones(1*(R+r),1) crisis1A(1:1*(R+r)) crisis2A(1:1*(R+r)) crisis3A(1:1*(R+r)) LOGtimetrendA(1:1*(R+r)) XA{m}(1:1*(R+r),:)];
                        elseif m>(M-Spl)
                                 X1=[ones(SN*(R+r),1) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) crisis3(1:SN*(R+r)) LOGtimetrend(1:SN*(R+r)) incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag}];
                                end
                    elseif lag==2
                        if m<=(M-Spl)
                        X1=[ones(SN*(R+r),1) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) crisis3(1:SN*(R+r)) LOGtimetrend(1:SN*(R+r)) X{m}(1:SN*(R+r),:) LAGco2pc(1:SN*(R+r),:)]; 
                                            X1A=[ones(1*(R+r),1) crisis1A(1:1*(R+r)) crisis2A(1:1*(R+r)) crisis3A(1:1*(R+r)) LOGtimetrendA(1:1*(R+r)) XA{m}(1:1*(R+r),:) LAGco2pcA(1:1*(R+r),:)]; 
                                     
                        elseif m>(M-Spl)
                         X1=[ones(SN*(R+r),1) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) crisis3(1:SN*(R+r)) LOGtimetrend(1:SN*(R+r)) incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(R+r),:)];    
                        end
                    end
                    
                  elseif Rr-(R+r)>11
                      
                       if lag==1
                        if m<=(M-Spl)
                        X1=[ones(SN*(R+r),1) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) LOGtimetrend(1:SN*(R+r)) X{m}(1:SN*(R+r),:)];
                                        X1A=[ones(1*(R+r),1) crisis1A(1:1*(R+r)) crisis2A(1:1*(R+r)) LOGtimetrendA(1:1*(R+r)) XA{m}(1:1*(R+r),:)];
                        elseif m>(M-Spl)
                                 X1=[ones(SN*(R+r),1) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) LOGtimetrend(1:SN*(R+r)) incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag}];
                                end
                    elseif lag==2
                        if m<=(M-Spl)
                        X1=[ones(SN*(R+r),1) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r))  LOGtimetrend(1:SN*(R+r)) X{m}(1:SN*(R+r),:) LAGco2pc(1:SN*(R+r),:)]; 
                                                X1A=[ones(1*(R+r),1) crisis1A(1:1*(R+r)) crisis2A(1:1*(R+r))  LOGtimetrendA(1:1*(R+r)) XA{m}(1:1*(R+r),:) LAGco2pcA(1:1*(R+r),:)];
                        elseif m>(M-Spl)
                         X1=[ones(SN*(R+r),1) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) LOGtimetrend(1:SN*(R+r)) incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(R+r),:)];    
                        end
                    end
                end
                    [nu,ka]=size(X1);
                        [b{l},a,res]=regress(co2pc(1:SN*(R+r),:),X1);
                    
                     b_n=size(b{l},1);
                    if Rr-(R+r)<=11
                      b{l}=b{l}([1,(5):b_n]);
                    elseif Rr-(R+r)>11
                      b{l}=b{l}([1,(4):b_n])  ;
                    end
                    clear b_n;
                              
                    b_n=size(b{l},1);
                    
                    y_m=(co2pc(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN));
                    if m<=(M-Spl)
                                                     [nuA,kaA]=size(X1A);
                                                        [bA{l},aA,resA]=regress(co2pcA(1:1*(R+r),:),X1A);

                                                     b_nA=size(bA{l},1);
                                                    if Rr-(R+r)<=11
                                                      bA{l}=bA{l}([1,(5):b_nA]);
                                                    elseif Rr-(R+r)>11
                                                      bA{l}=bA{l}([1,(4):b_nA])  ;
                                                    end
                                                    clear b_nA;
                                                              
                                                    b_nA=size(bA{l},1);

                                                    y_mA=(co2pcA(1*(R+r+theta-1)+1:1*(R+r+theta-1)+1));
                    
                    end
                    if lag==1
                        if m<=(M-Spl)
                        y_hat_m=[ones(SN,1).*b{l}(1)+LOGtimetrend(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN).*b{l}(1+1)+Z{m}((SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN),:)*b{l}(1+1+1:b_n)];
                                             y_hat_mA=[ones(1,1).*bA{l}(1)+LOGtimetrendA(1*(R+r+theta-1)+1:1*(R+r+theta-1)+1).*bA{l}(1+1)+ZA{m}((1*(R+r+theta-1)+1:1*(R+r+theta-1)+1),:)*bA{l}(1+1+1:b_nA)];
                        
                        elseif m>(M-Spl)
                                income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(1+1+1:b_n);
                              y_hat_m=[ones(SN,1).*b{l}(1)+LOGtimetrend(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN).*b{l}(1+1)+X{m}((SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN),:).*income_b];
                                end 
                    elseif lag==2
                        if unc==1
                        if m<=(M-Spl)
                        y_hat_m=[ones(SN,1).*b{l}(1)+LOGtimetrend(SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN).*b{l}(1+1)+Z{m}((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(1+1+1:b_n-SN)+LAGco2pc((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                        for t=2:theta
                        y_hat_m=[ones(SN,1).*b{l}(1)+LOGtimetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(1+1)+Z{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(1+1+1:b_n-SN)+y_hat_m.*b{l}(b_n-SN+1:b_n)];
                        end     
                                             y_hat_mA=[ones(1,1).*bA{l}(1)+LOGtimetrendA(1*(R+r+1-1)+1:1*(R+r+1-1)+1).*bA{l}(1+1)+ZA{m}((1*(R+r+1-1)+1:1*(R+r+1-1)+1),:)*bA{l}(1+1+1:b_nA-1)+LAGco2pcA((1*(R+r+1-1)+1:1*(R+r+1-1)+1),:)*bA{l}(b_nA-1+1:b_nA)];
                                            for t=2:theta
                                            y_hat_mA=[ones(1,1).*bA{l}(1)+LOGtimetrendA(1*(R+r+t-1)+1:1*(R+r+t-1)+1).*bA{l}(1+1)+ZA{m}((1*(R+r+t-1)+1:1*(R+r+t-1)+1),:)*bA{l}(1+1+1:b_nA-1)+y_hat_mA.*bA{l}(b_nA-1+1:b_nA)];
                                            end    
                        
                         elseif m>(M-Spl)
                                income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(1+1+1:b_n-SN);
                         y_hat_m=[ones(SN,1).*b{l}(1)+LOGtimetrend(SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN).*b{l}(1+1)+X{m}((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:).*income_b+LAGco2pc((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                        for t=2:theta
                        y_hat_m=[ones(SN,1).*b{l}(1)+LOGtimetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(1+1)+X{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:).*income_b+y_hat_m.*b{l}(b_n-SN+1:b_n)];
                        end
                        end
                        elseif unc==2
                                                if m<=(M-Spl)
                        t=theta;
                        y_hat_m=[ones(SN,1).*b{l}(1)+LOGtimetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(1+1)+Z{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(1+1+1:b_n-SN)+LAGco2pc((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                            
                                             t=theta;
                                            y_hat_mA=[ones(1,1).*bA{l}(1)+LOGtimetrendA(1*(R+r+t-1)+1:1*(R+r+t-1)+1).*bA{l}(1+1)+ZA{m}((1*(R+r+t-1)+1:1*(R+r+t-1)+1),:)*bA{l}(1+1+1:b_nA-1)+LAGco2pcA((1*(R+r+t-1)+1:1*(R+r+t-1)+1),:)*bA{l}(b_nA-1+1:b_nA)];
                                                   
                        
                         elseif m>(M-Spl)
                                income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(1+1+1:b_n-SN);
                        t=theta;
                        y_hat_m=[ones(SN,1).*b{l}(1)+LOGtimetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(1+1)+X{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:).*income_b+LAGco2pc((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                        
                    end
                        end    
                    end
%----------------------------------forecasts for each model from Rr+1 bis YEARS+YEARSFOR           
if  r==N
    if lag==1
        if m<=(M-Spl)
            X1=[ones(SN*(Rr),1) crisis1(1:SN*(Rr)) crisis2(1:SN*(Rr)) crisis3(1:SN*(Rr)) LOGtimetrend(1:SN*(Rr)) X{m}(1:SN*(Rr),:)];
        elseif m>(M-Spl)
            X1=[ones(SN*(Rr),1) crisis1(1:SN*(Rr)) crisis2(1:SN*(Rr)) crisis3(1:SN*(Rr)) LOGtimetrend(1:SN*(Rr)) incomespline_{N+1,(m-(M-Spl)+(10-Spl)),logs,lag}];
        end
    elseif lag==2
        if m<=(M-Spl)
            X1=[ones(SN*(Rr),1) crisis1(1:SN*(Rr)) crisis2(1:SN*(Rr)) crisis3(1:SN*(Rr)) LOGtimetrend(1:SN*(Rr)) X{m}(1:SN*(Rr),:) LAGco2pc(1:SN*(Rr),:)];
        elseif m>(M-Spl)
            X1=[ones(SN*(Rr),1) crisis1(1:SN*(Rr)) crisis2(1:SN*(Rr)) crisis3(1:SN*(Rr)) LOGtimetrend(1:SN*(Rr)) incomespline_{N+1,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(Rr),:)];
        end
    end
clear e n k

            [b{l},a,e,int,stat]=regress(co2pc(1:SN*(Rr),:),X1);
            [n,k]=size(X1);
            %SIC
            SIC(1,l)=[log((e'*e)/(n))+((k/n)*log(n))];
            %AIC
            AIC(1,l)=[log((e'*e)/(n))+(2*k/n)];
            %simple R2
            R_2(1,l)=stat(1,1);
            % adjusted R2
            R2adj(1,l)=1-((n-1)/(n-k))*(1-R_2(1,l));

    b_n=size(b{l},1);

    b{l}=b{l}([1,(5):b_n]);

    clear b_n;
    
    b_n=size(b{l},1);


    for c=Cc:CC
        for YF=1:YEARSFOR;
            if lag==1
                if m<=(M-Spl)
                    y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+LOGtimetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(1+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(1+1+1:b_n)];
                elseif m>(M-Spl)
                    income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(1+1+1:b_n);
                    y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+LOGtimetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(1+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b];
                end
            elseif lag==2 
                if unc==1
                if m<=(M-Spl)
                    if YF==1
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+LOGtimetrend(SN*(Rr+1-1)+1:SN*(Rr+1-1)+SN).*b{l}(1+1)+Y{m,c}((SN*(1-1)+1:SN*(1-1)+SN),:)*b{l}(1+1+1:b_n-SN)+co2pc((SN*(Rr-1)+1:SN*(Rr-1)+SN)).*b{l}(b_n-SN+1:b_n)];
                    end
                    if YF>1
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+LOGtimetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(1+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(1+1+1:b_n-SN)+y_hat{c,l}(:,YF-1).*b{l}(b_n-SN+1:b_n)];
                    end
                elseif m>(M-Spl)
                    if YF==1
                        income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(1+1+1:b_n-SN);
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+LOGtimetrend(SN*(Rr+1-1)+1:SN*(Rr+1-1)+SN).*b{l}(1+1)+Y{m,c}((SN*(1-1)+1:SN*(1-1)+SN),:).*income_b+co2pc((SN*(Rr-1)+1:SN*(Rr-1)+SN)).*b{l}(b_n-SN+1:b_n)];
                    end
                    if YF>1
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+LOGtimetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(1+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b+y_hat{c,l}(:,YF-1).*b{l}(b_n-SN+1:b_n)];
                    end
                end
                elseif unc==2
                if m<=(M-Spl)
                
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+LOGtimetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(1+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(1+1+1:b_n-SN)+co2pc((SN*(Rr-1-theta+YF)+1:SN*(Rr-1-theta+YF)+SN)).*b{l}(b_n-SN+1:b_n)];
                    
                elseif m>(M-Spl)
                    
                        income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(1+1+1:b_n-SN);
                    
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+LOGtimetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(1+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b+co2pc((SN*(Rr-1-theta+YF)+1:SN*(Rr-1-theta+YF)+SN)).*b{l}(b_n-SN+1:b_n)];
                   
                end
                end
            end
        end
               if logs==2 | logs==4
               y_hat{c,l}=exp(y_hat{c,l}+(e'*e)/(n-k)/2);
               end
    end
end
%---------------------------------
                    
                    if logs==1 | logs==3 
                        f_hat(r,l)=(ones(SN,1)'*[([y_m]-y_hat_m).^2]);
                        f_hatA(r,l)=(ones(SN,1)'*[([y_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))]-y_hat_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))).^2]);
                       
                       if m<=(M-Spl) 
                       f_hatagg(r,l)=[(((ones(SN,1)'*[y_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))])/popA(1*(R+r+theta-1)+1:1*(R+r+theta)))-((ones(SN,1)'*(y_hat_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))))/popA(1*(R+r+theta-1)+1:1*(R+r+theta)))).^2];
                        f_hatAagg(r,l)=([(ones(SN,1)'*[y_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))]-(ones(SN,1)'*(y_hat_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))))).^2]);
                        
                        f_hataggA(r,l)=([([y_mA]-y_hat_mA).^2]);
                        f_hatAaggA(r,l)=([([y_mA.*popA(1*(R+r+theta-1)+1:1*(R+r+theta))]-y_hat_mA.*popA(1*(R+r+theta-1)+1:1*(R+r+theta))).^2]);
                       end
                       elseif logs==2  | logs==4
                        f_hat(r,l)=(ones(SN,1)'*[(exp(y_m)-exp(y_hat_m+(res'*res)/(nu-ka)/2)).^2]);
                        f_hatA(r,l)=(ones(SN,1)'*[(exp(y_m).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))-exp(y_hat_m+(res'*res)/(nu-ka)/2).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))).^2]);
                        
                        if m<=(M-Spl)
                        f_hatagg(r,l)=[(((ones(SN,1)'*[exp(y_m).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))])/popA(1*(R+r+theta-1)+1:1*(R+r+theta)))-((ones(SN,1)'*(exp(y_hat_m+(res'*res)/(nu-ka)/2).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))))/popA(1*(R+r+theta-1)+1:1*(R+r+theta)))).^2];
                        f_hatAagg(r,l)=([(ones(SN,1)'*[exp(y_m).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))]-(ones(SN,1)'*(exp(y_hat_m+(res'*res)/(nu-ka)/2).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))))).^2]);
                        
                        f_hataggA(r,l)=([([exp(y_mA)]-exp(y_hat_mA+(resA'*resA)/(nuA-kaA)/2)).^2]);
                        f_hatAaggA(r,l)=([([exp(y_mA).*popA(1*(R+r+theta-1)+1:1*(R+r+theta))]-exp(y_hat_mA+(resA'*resA)/(nuA-kaA)/2).*popA(1*(R+r+theta-1)+1:1*(R+r+theta))).^2]);
                        end
                        end;
                    clear X1 x_time btime y_m b_n res nu ka y_hat_m y_hat_mA y_mA b_nA resA nuA kaA 
                end;
                
            end;
% 18th loop INCLUDING CRISIS DUMMYS and LOG-TIMETREND   NO STATE FIXED EFFECTS---BUT QUALITY DUMMIES 
            for q=1:QDN    
                 %waitbar(l/w)
                for m=1:MM 
                     l=l+1;
                    for r=1:N
                        Laufzeit=Laufzeit-1;
                        clc
                        disp(['Regressions left: ',num2str(Laufzeit)])
                        
                      if R+r>=32  
                        if lag==1
                            if m<=(M-Spl)
                            X1=[ones(SN*(R+r),1)  crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) crisis3(1:SN*(R+r)) QD{q}(1:SN*(R+r),:) LOGtimetrend(1:SN*(R+r)) X{m}(1:SN*(R+r),:)];
                            elseif m>(M-Spl)
                                 X1=[ones(SN*(R+r),1)  crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) crisis3(1:SN*(R+r)) QD{q}(1:SN*(R+r),:) LOGtimetrend(1:SN*(R+r)) incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag}];
                                end
                        elseif lag==2
                            if m<=(M-Spl)
                            X1=[ones(SN*(R+r),1)  crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) crisis3(1:SN*(R+r)) QD{q}(1:SN*(R+r),:) LOGtimetrend(1:SN*(R+r)) X{m}(1:SN*(R+r),:) LAGco2pc(1:SN*(R+r),:)];
                            elseif m>(M-Spl)
                            X1=[ones(SN*(R+r),1)  crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) crisis3(1:SN*(R+r)) QD{q}(1:SN*(R+r),:) LOGtimetrend(1:SN*(R+r)) incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(R+r),:)];    
                            end
                        end
                        
                     elseif R+r<32
                         
                           if lag==1
                            if m<=(M-Spl)
                            X1=[ones(SN*(R+r),1)  crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r))  QD{q}(1:SN*(R+r),:) LOGtimetrend(1:SN*(R+r)) X{m}(1:SN*(R+r),:)];
                            elseif m>(M-Spl)
                                 X1=[ones(SN*(R+r),1)  crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r))  QD{q}(1:SN*(R+r),:) LOGtimetrend(1:SN*(R+r)) incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag}];
                                end
                        elseif lag==2
                            if m<=(M-Spl)
                            X1=[ones(SN*(R+r),1)  crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r))  QD{q}(1:SN*(R+r),:) LOGtimetrend(1:SN*(R+r)) X{m}(1:SN*(R+r),:) LAGco2pc(1:SN*(R+r),:)];
                            elseif m>(M-Spl)
                            X1=[ones(SN*(R+r),1)  crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) QD{q}(1:SN*(R+r),:) LOGtimetrend(1:SN*(R+r)) incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(R+r),:)];    
                            end
                        end
                    end
                        [nu,ka]=size(X1);
                        [b{l},a,res]=regress(co2pc(1:SN*(R+r),:),X1);
                        
                     b_n=size(b{l},1);
                    if R+r>=32
                      b{l}=b{l}([1,(5):b_n]);
                    elseif R+r<32
                      b{l}=b{l}([1,(4):b_n])  ;
                    end
                    clear b_n;
                   
                        b_n=size(b{l},1);
                        q_k=size(QD{q},2);
                        
                        y_m=(co2pc(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN));
                        if lag==1
                            if m<=(M-Spl)
                            y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+LOGtimetrend(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN).*b{l}(q_k+2)+Z{m}((SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN),:)*b{l}(q_k+2+1:b_n)];
                            elseif m>(M-Spl)
                                income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(q_k+2+1:b_n);
                              y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+LOGtimetrend(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN).*b{l}(q_k+2)+X{m}((SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN),:).*income_b];
                                end 
                        elseif lag==2
                            if unc==1
                            if m<=(M-Spl)
                            y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+LOGtimetrend(SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN).*b{l}(q_k+2)+Z{m}((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(q_k+2+1:b_n-SN)+LAGco2pc((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                            for t=2:theta
                            y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+LOGtimetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(q_k+2)+Z{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(q_k+2+1:b_n-SN)+y_hat_m.*b{l}(b_n-SN+1:b_n)];
                            end     
                             elseif m>(M-Spl)
                                income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(q_k+2+1:b_n-SN);
                            y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+LOGtimetrend(SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN).*b{l}(q_k+2)+X{m}((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:).*income_b+LAGco2pc((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                            for t=2:theta
                            y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+LOGtimetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(q_k+2)+X{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:).*income_b+y_hat_m.*b{l}(b_n-SN+1:b_n)];
                            end  
                            end
                            elseif unc==2
                                                        if m<=(M-Spl)
                            t=theta;
                            y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+LOGtimetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(q_k+2)+Z{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(q_k+2+1:b_n-SN)+LAGco2pc((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                                 
                             elseif m>(M-Spl)
                                income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(q_k+2+1:b_n-SN);
                           t=theta;
                            y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+LOGtimetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(q_k+2)+X{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:).*income_b+LAGco2pc((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                              
                        end
                            end
                            
                        end
%----------------------------------forecasts for each model from Rr+1 bis YEARS+YEARSFOR           
if  r==N
    if lag==1
        if m<=(M-Spl)
            X1=[ones(SN*(Rr),1)  crisis1(1:SN*(Rr)) crisis2(1:SN*(Rr)) crisis3(1:SN*(Rr)) QD{q}(1:SN*(Rr),:) LOGtimetrend(1:SN*(Rr)) X{m}(1:SN*(Rr),:)];
        elseif m>(M-Spl)
            X1=[ones(SN*(Rr),1)  crisis1(1:SN*(Rr)) crisis2(1:SN*(Rr)) crisis3(1:SN*(Rr)) QD{q}(1:SN*(Rr),:) LOGtimetrend(1:SN*(Rr)) incomespline_{N+1,(m-(M-Spl)+(10-Spl)),logs,lag}];
        end
    elseif lag==2
        if m<=(M-Spl)
            X1=[ones(SN*(Rr),1)  crisis1(1:SN*(Rr)) crisis2(1:SN*(Rr)) crisis3(1:SN*(Rr)) QD{q}(1:SN*(Rr),:) LOGtimetrend(1:SN*(Rr)) X{m}(1:SN*(Rr),:) LAGco2pc(1:SN*(Rr),:)];
        elseif m>(M-Spl)
            X1=[ones(SN*(Rr),1)  crisis1(1:SN*(Rr)) crisis2(1:SN*(Rr)) crisis3(1:SN*(Rr)) QD{q}(1:SN*(Rr),:) LOGtimetrend(1:SN*(Rr)) incomespline_{N+1,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(Rr),:)];
        end
    end

clear e n k
            [b{l},a,e,int,stat]=regress(co2pc(1:SN*(Rr),:),X1);
            [n,k]=size(X1);
            %SIC
            SIC(1,l)=[log((e'*e)/(n))+((k/n)*log(n))];
            %AIC
            AIC(1,l)=[log((e'*e)/(n))+(2*k/n)];
            %simple R2
            R_2(1,l)=stat(1,1);
            % adjusted R2
            R2adj(1,l)=1-((n-1)/(n-k))*(1-R_2(1,l));

    b_n=size(b{l},1);

    b{l}=b{l}([1,(5):b_n]);

    clear b_n;
    
    b_n=size(b{l},1);
    q_k=size(QD{q},2);



    for c=Cc:CC
        for YF=1:YEARSFOR;
            if lag==1
                if m<=(M-Spl)
                    y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+LOGtimetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(q_k+2)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(q_k+2+1:b_n)];
                elseif m>(M-Spl)
                    income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(q_k+2+1:b_n);
                    y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+LOGtimetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(q_k+2)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b];
                end
            elseif lag==2 
                if unc==1
                if m<=(M-Spl)
                    if YF==1
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+LOGtimetrend(SN*(Rr+1-1)+1:SN*(Rr+1-1)+SN).*b{l}(q_k+2)+Y{m,c}((SN*(1-1)+1:SN*(1-1)+SN),:)*b{l}(q_k+2+1:b_n-SN)+co2pc((SN*(Rr-1)+1:SN*(Rr-1)+SN)).*b{l}(b_n-SN+1:b_n)];
                    end
                    if YF>1
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+LOGtimetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(q_k+2)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(q_k+2+1:b_n-SN)+y_hat{c,l}(:,YF-1).*b{l}(b_n-SN+1:b_n)];
                    end
                elseif m>(M-Spl)
                    if YF==1
                        income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(q_k+2+1:b_n-SN);
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+LOGtimetrend(SN*(Rr+1-1)+1:SN*(Rr+1-1)+SN).*b{l}(q_k+2)+Y{m,c}((SN*(1-1)+1:SN*(1-1)+SN),:).*income_b+co2pc((SN*(Rr-1)+1:SN*(Rr-1)+SN)).*b{l}(b_n-SN+1:b_n)];
                    end
                    if YF>1
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+LOGtimetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(q_k+2)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b+y_hat{c,l}(:,YF-1).*b{l}(b_n-SN+1:b_n)];
                    end
                end
                elseif unc==2
                if m<=(M-Spl)
                   
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+LOGtimetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(q_k+2)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(q_k+2+1:b_n-SN)+co2pc((SN*(Rr-1-theta+YF)+1:SN*(Rr-1-theta+YF)+SN)).*b{l}(b_n-SN+1:b_n)];
                    
                elseif m>(M-Spl)
                    
                        income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(q_k+2+1:b_n-SN);
                 
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+LOGtimetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(q_k+2)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b+co2pc((SN*(Rr-1-theta+YF)+1:SN*(Rr-1-theta+YF)+SN)).*b{l}(b_n-SN+1:b_n)];
                    
                end
                end
            end
         end
               if logs==2 || logs==4
               y_hat{c,l}=exp(y_hat{c,l}+(e'*e)/(n-k)/2);
               end
    end
end
%---------------------------------
                  
                        if logs==1 || logs==3 
                            f_hat(r,l)=(ones(SN,1)'*[([y_m]-y_hat_m).^2]);
                            f_hatA(r,l)=(ones(SN,1)'*[([y_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))]-y_hat_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))).^2]);
                            
                        elseif logs==2 || logs==4
                            f_hat(r,l)=(ones(SN,1)'*[(exp(y_m)-exp(y_hat_m+(res'*res)/(nu-ka)/2)).^2]);
                            f_hatA(r,l)=(ones(SN,1)'*[(exp(y_m).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))-exp(y_hat_m+(res'*res)/(nu-ka)/2).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))).^2]);
                           
                        end;
                        clear X1 x_time btime y_m b_n res nu ka 
                    end;
                    
                end;
            end;           
                       
% 19th loop INCLUDING State fixed effects  and CRISIS DUMMYS and TIMETREND
             %waitbar(l/w)
            for m=1:MM    
                 l=l+1;
                for r=1:N
                    Laufzeit=Laufzeit-1;
                    clc
                    disp(['Regressions left: ',num2str(Laufzeit)])
                    
                  if R+r>=32  %since we can only take crisis3 in when including at least year 1990 (dummy is for 90-91)  
                    if lag==1
                        if m<=(M-Spl)
                        X1=[state(1:SN*(R+r),:) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) crisis3(1:SN*(R+r)) timetrend(1:SN*(R+r)) X{m}(1:SN*(R+r),:)];
                        elseif m>(M-Spl)
                                 X1=[state(1:SN*(R+r),:) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) crisis3(1:SN*(R+r)) timetrend(1:SN*(R+r)) incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag}];
                                end
                    elseif lag==2
                        if m<=(M-Spl)
                        X1=[state(1:SN*(R+r),:) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) crisis3(1:SN*(R+r)) timetrend(1:SN*(R+r)) X{m}(1:SN*(R+r),:) LAGco2pc(1:SN*(R+r),:)]; 
                        elseif m>(M-Spl)
                         X1=[state(1:SN*(R+r),:) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) crisis3(1:SN*(R+r)) timetrend(1:SN*(R+r)) incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(R+r),:)];     
                        end
                   end
                elseif R+r<32
                    if lag==1
                        if m<=(M-Spl)
                        X1=[state(1:SN*(R+r),:) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r))  timetrend(1:SN*(R+r)) X{m}(1:SN*(R+r),:)];
                        elseif m>(M-Spl)
                                 X1=[state(1:SN*(R+r),:) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r))  timetrend(1:SN*(R+r)) incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag}];
                                end
                    elseif lag==2
                        if m<=(M-Spl)
                        X1=[state(1:SN*(R+r),:) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r))  timetrend(1:SN*(R+r)) X{m}(1:SN*(R+r),:) LAGco2pc(1:SN*(R+r),:)]; 
                        elseif m>(M-Spl)
                         X1=[state(1:SN*(R+r),:) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) timetrend(1:SN*(R+r)) incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(R+r),:)];     
                        end
                   end

                end
                    [nu,ka]=size(X1);
                        [b{l},a,res]=regress(co2pc(1:SN*(R+r),:),X1);
                     b_n=size(b{l},1);
                    if R+r>=32
                      b{l}=b{l}([1:SN,(SN+4):b_n]);
                    elseif R+r<32
                      b{l}=b{l}([1:SN,(SN+3):b_n])  ;
                    end
                    clear b_n;
                    
                             
                    b_n=size(b{l},1);
                    
                    y_m=(co2pc(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN));
                    if lag==1
                        if m<=(M-Spl)
                        y_hat_m=[eye(SN,SN)*b{l}(1:SN)+timetrend(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN).*b{l}(SN+1)+Z{m}((SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN),:)*b{l}(SN+1+1:b_n)];
                        elseif m>(M-Spl)
                                income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(SN+1+1:b_n);
                              y_hat_m=[eye(SN,SN)*b{l}(1:SN)+timetrend(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN).*b{l}(SN+1)+X{m}((SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN),:).*income_b];
                                end 
                    elseif lag==2 
                        if unc==1
                        if m<=(M-Spl)
                        y_hat_m=[eye(SN,SN)*b{l}(1:SN)+timetrend(SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN).*b{l}(SN+1)+Z{m}((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(SN+1+1:b_n-SN)+LAGco2pc((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                        for t=2:theta
                        y_hat_m=[eye(SN,SN)*b{l}(1:SN)+timetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(SN+1)+Z{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(SN+1+1:b_n-SN)+y_hat_m.*b{l}(b_n-SN+1:b_n)];
                        end        
                         elseif m>(M-Spl)
                                income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(SN+1+1:b_n-SN);
                        y_hat_m=[eye(SN,SN)*b{l}(1:SN)+timetrend(SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN).*b{l}(SN+1)+X{m}((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:).*income_b+LAGco2pc((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                        for t=2:theta
                        y_hat_m=[eye(SN,SN)*b{l}(1:SN)+timetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(SN+1)+X{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:).*income_b+y_hat_m.*b{l}(b_n-SN+1:b_n)];
                        end  
                        end
                        elseif unc==2
                                                if m<=(M-Spl)
                       t=theta;
                        y_hat_m=[eye(SN,SN)*b{l}(1:SN)+timetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(SN+1)+Z{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(SN+1+1:b_n-SN)+LAGco2pc((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                                
                         elseif m>(M-Spl)
                                income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(SN+1+1:b_n-SN);
                        t=theta;
                        y_hat_m=[eye(SN,SN)*b{l}(1:SN)+timetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(SN+1)+X{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:).*income_b+LAGco2pc((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                         
                    end
                        end
                        
                    end
%----------------------------------forecasts for each model from Rr+1 bis YEARS+YEARSFOR           
if  r==N
    if lag==1
        if m<=(M-Spl)
            X1=[state(1:SN*(Rr),:) crisis1(1:SN*(Rr)) crisis2(1:SN*(Rr)) crisis3(1:SN*(Rr)) timetrend(1:SN*(Rr)) X{m}(1:SN*(Rr),:)];
        elseif m>(M-Spl)
            X1=[state(1:SN*(Rr),:) crisis1(1:SN*(Rr)) crisis2(1:SN*(Rr)) crisis3(1:SN*(Rr)) timetrend(1:SN*(Rr)) incomespline_{N+1,(m-(M-Spl)+(10-Spl)),logs,lag}];
        end
    elseif lag==2
        if m<=(M-Spl)
            X1=[state(1:SN*(Rr),:) crisis1(1:SN*(Rr)) crisis2(1:SN*(Rr)) crisis3(1:SN*(Rr)) timetrend(1:SN*(Rr)) X{m}(1:SN*(Rr),:) LAGco2pc(1:SN*(Rr),:)];
        elseif m>(M-Spl)
            X1=[state(1:SN*(Rr),:) crisis1(1:SN*(Rr)) crisis2(1:SN*(Rr)) crisis3(1:SN*(Rr)) timetrend(1:SN*(Rr)) incomespline_{N+1,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(Rr),:)];
        end
    end
clear e n k
            [b{l},a,e,int,stat]=regress(co2pc(1:SN*(Rr),:),X1);
            [n,k]=size(X1);
            %SIC
            SIC(1,l)=[log((e'*e)/(n))+((k/n)*log(n))];
            %AIC
            AIC(1,l)=[log((e'*e)/(n))+(2*k/n)];
            %simple R2
            R_2(1,l)=stat(1,1);
            % adjusted R2
            R2adj(1,l)=1-((n-1)/(n-k))*(1-R_2(1,l));
    b_n=size(b{l},1);

    b{l}=b{l}([1:SN,(SN+4):b_n]);

    clear b_n;

    
    b_n=size(b{l},1);

    for c=Cc:CC
        for YF=1:YEARSFOR;
            if lag==1
                if m<=(M-Spl)
                    y_hat{c,l}(:,YF)=[b{l}(1:SN)+timetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(SN+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(SN+1+1:b_n)];
                elseif m>(M-Spl)
                    income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(SN+1+1:b_n);
                    y_hat{c,l}(:,YF)=[b{l}(1:SN)+timetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(SN+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b];
                end
            elseif lag==2 
                if unc==1
                if m<=(M-Spl)
                    if YF==1
                        y_hat{c,l}(:,YF)=[b{l}(1:SN)+timetrend(SN*(Rr+1-1)+1:SN*(Rr+1-1)+SN).*b{l}(SN+1)+Y{m,c}((SN*(1-1)+1:SN*(1-1)+SN),:)*b{l}(SN+1+1:b_n-SN)+co2pc((SN*(Rr-1)+1:SN*(Rr-1)+SN)).*b{l}(b_n-SN+1:b_n)];
                    end
                    if YF>1
                        y_hat{c,l}(:,YF)=[b{l}(1:SN)+timetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(SN+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(SN+1+1:b_n-SN)+y_hat{c,l}(:,YF-1).*b{l}(b_n-SN+1:b_n)];
                    end
                elseif m>(M-Spl)
                    if YF==1
                        income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(SN+1+1:b_n-SN);
                        y_hat{c,l}(:,YF)=[b{l}(1:SN)+timetrend(SN*(Rr+1-1)+1:SN*(Rr+1-1)+SN).*b{l}(SN+1)+Y{m,c}((SN*(1-1)+1:SN*(1-1)+SN),:).*income_b+co2pc((SN*(Rr-1)+1:SN*(Rr-1)+SN)).*b{l}(b_n-SN+1:b_n)];
                    end
                    if YF>1
                        y_hat{c,l}(:,YF)=[b{l}(1:SN)+timetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(SN+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b+y_hat{c,l}(:,YF-1).*b{l}(b_n-SN+1:b_n)];
                    end
                end
                elseif unc==2
                if m<=(M-Spl)
                    
                        y_hat{c,l}(:,YF)=[b{l}(1:SN)+timetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(SN+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(SN+1+1:b_n-SN)+co2pc((SN*(Rr-1-theta+YF)+1:SN*(Rr-1-theta+YF)+SN)).*b{l}(b_n-SN+1:b_n)];
                    
                elseif m>(M-Spl)
                    
                        income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(SN+1+1:b_n-SN);
                      
                        y_hat{c,l}(:,YF)=[b{l}(1:SN)+timetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(SN+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b+co2pc((SN*(Rr-1-theta+YF)+1:SN*(Rr-1-theta+YF)+SN)).*b{l}(b_n-SN+1:b_n)];
                    
                end
                end
            end
         end
               if logs==2 | logs==4
               y_hat{c,l}=exp(y_hat{c,l}+(e'*e)/(n-k)/2);
               end
    end
end
%---------------------------------
                    
                    if logs==1 | logs==3 
                        f_hat(r,l)=(ones(SN,1)'*[([y_m]-y_hat_m).^2]);
                        f_hatA(r,l)=(ones(SN,1)'*[([y_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))]-y_hat_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))).^2]);
                        
                    elseif logs==2  | logs==4
                        f_hat(r,l)=(ones(SN,1)'*[(exp(y_m)-exp(y_hat_m+(res'*res)/(nu-ka)/2)).^2]);
                        f_hatA(r,l)=(ones(SN,1)'*[(exp(y_m).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))-exp(y_hat_m+(res'*res)/(nu-ka)/2).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))).^2]);
                       
                    end;
                    clear X1 x_time btime y_m b_n y_hat_m res nu ka 
                end;
                
            end;
            
            
% 20th loop INCLUDING CRISIS DUMMYS and TIMETREND      NO STATE FIXED EFFECTS  
%INCLUDING US AGG REGRESSIONS 

            for m=1:MM 
                 l=l+1;
                for r=1:N
                    Laufzeit=Laufzeit-1;
                    clc
                    disp(['Regressions left: ',num2str(Laufzeit)])
                    
                  if  Rr-(R+r)<=11 
                    if lag==1
                        if m<=(M-Spl)
                        X1=[ones(SN*(R+r),1) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) crisis3(1:SN*(R+r)) timetrend(1:SN*(R+r)) X{m}(1:SN*(R+r),:)];
                                            X1A=[ones(1*(R+r),1) crisis1A(1:1*(R+r)) crisis2A(1:1*(R+r)) crisis3A(1:1*(R+r)) timetrendA(1:1*(R+r)) XA{m}(1:1*(R+r),:)];
                        elseif m>(M-Spl)
                                 X1=[ones(SN*(R+r),1) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) crisis3(1:SN*(R+r)) timetrend(1:SN*(R+r)) incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag}];
                                end
                    elseif lag==2
                        if m<=(M-Spl)
                        X1=[ones(SN*(R+r),1) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) crisis3(1:SN*(R+r)) timetrend(1:SN*(R+r)) X{m}(1:SN*(R+r),:) LAGco2pc(1:SN*(R+r),:)]; 
                                                 X1A=[ones(1*(R+r),1) crisis1A(1:1*(R+r)) crisis2A(1:1*(R+r)) crisis3A(1:1*(R+r)) timetrendA(1:1*(R+r)) XA{m}(1:1*(R+r),:) LAGco2pcA(1:1*(R+r),:)]; 
                        elseif m>(M-Spl)
                         X1=[ones(SN*(R+r),1) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) crisis3(1:SN*(R+r)) timetrend(1:SN*(R+r)) incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(R+r),:)];    
                        end
                    end
                    
                  elseif Rr-(R+r)>11
                      
                       if lag==1
                        if m<=(M-Spl)
                        X1=[ones(SN*(R+r),1) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) timetrend(1:SN*(R+r)) X{m}(1:SN*(R+r),:)];
                                                 X1A=[ones(1*(R+r),1) crisis1A(1:1*(R+r)) crisis2A(1:1*(R+r)) timetrendA(1:1*(R+r)) XA{m}(1:1*(R+r),:)];
                        elseif m>(M-Spl)
                                 X1=[ones(SN*(R+r),1) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) timetrend(1:SN*(R+r)) incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag}];
                                end
                    elseif lag==2
                        if m<=(M-Spl)
                        X1=[ones(SN*(R+r),1) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) timetrend(1:SN*(R+r)) X{m}(1:SN*(R+r),:) LAGco2pc(1:SN*(R+r),:)]; 
                                                    X1A=[ones(1*(R+r),1) crisis1A(1:1*(R+r)) crisis2A(1:1*(R+r)) timetrendA(1:1*(R+r)) XA{m}(1:1*(R+r),:) LAGco2pcA(1:1*(R+r),:)];
                        elseif m>(M-Spl)
                         X1=[ones(SN*(R+r),1) crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) timetrend(1:SN*(R+r)) incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(R+r),:)];    
                        end
                    end
                end
                    [nu,ka]=size(X1);
                                    
                    [b{l},a,res]=regress(co2pc(1:SN*(R+r),:),X1);
                                    
                     b_n=size(b{l},1);
                                     
                    if Rr-(R+r)<=11
                      b{l}=b{l}([1,(5):b_n]);
                                   
                    elseif Rr-(R+r)>11
                      b{l}=b{l}([1,(4):b_n]);
                                     
                    end
                    clear b_n ;
                             
                    b_n=size(b{l},1);
                                    
                    
                    y_m=(co2pc(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN));
                                     
                                     if m<=(M-Spl)
                                         [nuA,kaA]=size(X1A);
                                         [bA{l},aA,resA]=regress(co2pcA(1:1*(R+r),:),X1A);
                                         b_nA=size(bA{l},1);
                                          if Rr-(R+r)<=11
                      
                                    bA{l}=bA{l}([1,(5):b_nA]);
                                         elseif Rr-(R+r)>11
                      
                                      bA{l}=bA{l}([1,(4):b_nA]);
                                        end
                                        clear b_nA
                                      b_nA=size(bA{l},1);   
                                      y_mA=(co2pcA(1*(R+r+theta-1)+1:1*(R+r+theta-1)+1));   
                                     end
                                     
                                     
                    if lag==1
                        if m<=(M-Spl)
                        y_hat_m=[ones(SN,1).*b{l}(1)+timetrend(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN).*b{l}(1+1)+Z{m}((SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN),:)*b{l}(1+1+1:b_n)];
                            y_hat_mA=[ones(1,1).*bA{l}(1)+timetrendA(1*(R+r+theta-1)+1:1*(R+r+theta-1)+1).*bA{l}(1+1)+ZA{m}((1*(R+r+theta-1)+1:1*(R+r+theta-1)+1),:)*bA{l}(1+1+1:b_nA)];
                        elseif m>(M-Spl)
                                income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(1+1+1:b_n);
                              y_hat_m=[ones(SN,1).*b{l}(1)+timetrend(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN).*b{l}(1+1)+X{m}((SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN),:).*income_b];
                                end 
                    elseif lag==2
                        if unc==1
                        if m<=(M-Spl)
                        y_hat_m=[ones(SN,1).*b{l}(1)+timetrend(SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN).*b{l}(1+1)+Z{m}((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(1+1+1:b_n-SN)+LAGco2pc((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                        for t=2:theta
                        y_hat_m=[ones(SN,1).*b{l}(1)+timetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(1+1)+Z{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(1+1+1:b_n-SN)+y_hat_m.*b{l}(b_n-SN+1:b_n)];
                        end      
                                y_hat_mA=[ones(1,1).*bA{l}(1)+timetrendA(1*(R+r+1-1)+1:1*(R+r+1-1)+1).*bA{l}(1+1)+ZA{m}((1*(R+r+1-1)+1:1*(R+r+1-1)+1),:)*bA{l}(1+1+1:b_nA-1)+LAGco2pcA((1*(R+r+1-1)+1:1*(R+r+1-1)+1),:)*bA{l}(b_nA-1+1:b_nA)];
                                for t=2:theta
                                y_hat_mA=[ones(1,1).*bA{l}(1)+timetrendA(1*(R+r+t-1)+1:1*(R+r+t-1)+1).*bA{l}(1+1)+ZA{m}((1*(R+r+t-1)+1:1*(R+r+t-1)+1),:)*bA{l}(1+1+1:b_nA-1)+y_hat_mA.*bA{l}(b_nA-1+1:b_nA)];
                                end      

                         elseif m>(M-Spl)
                                income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(1+1+1:b_n-SN);
                         y_hat_m=[ones(SN,1).*b{l}(1)+timetrend(SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN).*b{l}(1+1)+X{m}((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:).*income_b+LAGco2pc((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                        for t=2:theta
                        y_hat_m=[ones(SN,1).*b{l}(1)+timetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(1+1)+X{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:).*income_b+y_hat_m.*b{l}(b_n-SN+1:b_n)];
                        end
                        end
                        elseif unc==2
                                                if m<=(M-Spl)
                       t=theta;
                        y_hat_m=[ones(SN,1).*b{l}(1)+timetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(1+1)+Z{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(1+1+1:b_n-SN)+LAGco2pc((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                                                     
                                t=theta;
                                y_hat_mA=[ones(1,1).*bA{l}(1)+timetrendA(1*(R+r+t-1)+1:1*(R+r+t-1)+1).*bA{l}(1+1)+ZA{m}((1*(R+r+t-1)+1:1*(R+r+t-1)+1),:)*bA{l}(1+1+1:b_nA-1)+LAGco2pcA((1*(R+r+t-1)+1:1*(R+r+t-1)+1),:)*bA{l}(b_nA-1+1:b_nA)];
                                                     

                         elseif m>(M-Spl)
                                income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(1+1+1:b_n-SN);
                         t=theta;
                        y_hat_m=[ones(SN,1).*b{l}(1)+timetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(1+1)+X{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:).*income_b+LAGco2pc((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                        
                    end
                        end
                        
                    end
%----------------------------------forecasts for each model from Rr+1 bis YEARS+YEARSFOR           
if  r==N
    if lag==1
        if m<=(M-Spl)
            X1=[ones(SN*(Rr),1) crisis1(1:SN*(Rr)) crisis2(1:SN*(Rr)) crisis3(1:SN*(Rr)) timetrend(1:SN*(Rr)) X{m}(1:SN*(Rr),:)];
        elseif m>(M-Spl)
            X1=[ones(SN*(Rr),1) crisis1(1:SN*(Rr)) crisis2(1:SN*(Rr)) crisis3(1:SN*(Rr)) timetrend(1:SN*(Rr)) incomespline_{N+1,(m-(M-Spl)+(10-Spl)),logs,lag}];
        end
    elseif lag==2
        if m<=(M-Spl)
            X1=[ones(SN*(Rr),1) crisis1(1:SN*(Rr)) crisis2(1:SN*(Rr)) crisis3(1:SN*(Rr)) timetrend(1:SN*(Rr)) X{m}(1:SN*(Rr),:) LAGco2pc(1:SN*(Rr),:)];
        elseif m>(M-Spl)
            X1=[ones(SN*(Rr),1) crisis1(1:SN*(Rr)) crisis2(1:SN*(Rr)) crisis3(1:SN*(Rr)) timetrend(1:SN*(Rr)) incomespline_{N+1,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(Rr),:)];
        end
    end

clear e n k
            [b{l},a,e,int,stat]=regress(co2pc(1:SN*(Rr),:),X1);
            [n,k]=size(X1);
            %SIC
            SIC(1,l)=[log((e'*e)/(n))+((k/n)*log(n))];
            %AIC
            AIC(1,l)=[log((e'*e)/(n))+(2*k/n)];
            %simple R2
            R_2(1,l)=stat(1,1);
            % adjusted R2
            R2adj(1,l)=1-((n-1)/(n-k))*(1-R_2(1,l));

    b_n=size(b{l},1);

    b{l}=b{l}([1,(5):b_n]);

    clear b_n;
    
    b_n=size(b{l},1);

    for c=Cc:CC
        for YF=1:YEARSFOR;
            if lag==1
                if m<=(M-Spl)
                    y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+timetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(1+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(1+1+1:b_n)];
                elseif m>(M-Spl)
                    income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(1+1+1:b_n);
                    y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+timetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(1+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b];
                end
            elseif lag==2  
               if unc==1
                if m<=(M-Spl)
                    if YF==1
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+timetrend(SN*(Rr+1-1)+1:SN*(Rr+1-1)+SN).*b{l}(1+1)+Y{m,c}((SN*(1-1)+1:SN*(1-1)+SN),:)*b{l}(1+1+1:b_n-SN)+co2pc((SN*(Rr-1)+1:SN*(Rr-1)+SN)).*b{l}(b_n-SN+1:b_n)];
                    end
                    if YF>1
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+timetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(1+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(1+1+1:b_n-SN)+y_hat{c,l}(:,YF-1).*b{l}(b_n-SN+1:b_n)];
                    end
                elseif m>(M-Spl)
                    if YF==1
                        income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(1+1+1:b_n-SN);
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+timetrend(SN*(Rr+1-1)+1:SN*(Rr+1-1)+SN).*b{l}(1+1)+Y{m,c}((SN*(1-1)+1:SN*(1-1)+SN),:).*income_b+co2pc((SN*(Rr-1)+1:SN*(Rr-1)+SN)).*b{l}(b_n-SN+1:b_n)];
                    end
                    if YF>1
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+timetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(1+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b+y_hat{c,l}(:,YF-1).*b{l}(b_n-SN+1:b_n)];
                    end
                end
               elseif unc==2
               if m<=(M-Spl)
                   
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+timetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(1+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(1+1+1:b_n-SN)+co2pc((SN*(Rr-1-theta+YF)+1:SN*(Rr-1-theta+YF)+SN)).*b{l}(b_n-SN+1:b_n)];
                    
                elseif m>(M-Spl)
                   
                        income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(1+1+1:b_n-SN);
                      
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+timetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(1+1)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b+co2pc((SN*(Rr-1-theta+YF)+1:SN*(Rr-1-theta+YF)+SN)).*b{l}(b_n-SN+1:b_n)];
                    
                end
               end
            end
         end
               if logs==2 | logs==4
               y_hat{c,l}=exp(y_hat{c,l}+(e'*e)/(n-k)/2);
               end
    end
end
%---------------------------------
                    
                    if logs==1 | logs==3 
                        f_hat(r,l)=(ones(SN,1)'*[([y_m]-y_hat_m).^2]);
                        f_hatA(r,l)=(ones(SN,1)'*[([y_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))]-y_hat_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))).^2]);
                       
                       if m<=(M-Spl) 
                       f_hatagg(r,l)=[(((ones(SN,1)'*[y_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))])/popA(1*(R+r+theta-1)+1:1*(R+r+theta)))-((ones(SN,1)'*(y_hat_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))))/popA(1*(R+r+theta-1)+1:1*(R+r+theta)))).^2];
                        f_hatAagg(r,l)=([(ones(SN,1)'*[y_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))]-(ones(SN,1)'*(y_hat_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))))).^2]);
                        
                        f_hataggA(r,l)=([([y_mA]-y_hat_mA).^2]);
                        f_hatAaggA(r,l)=([([y_mA.*popA(1*(R+r+theta-1)+1:1*(R+r+theta))]-y_hat_mA.*popA(1*(R+r+theta-1)+1:1*(R+r+theta))).^2]);
                       end
                       elseif logs==2  | logs==4
                        f_hat(r,l)=(ones(SN,1)'*[(exp(y_m)-exp(y_hat_m+(res'*res)/(nu-ka)/2)).^2]);
                        f_hatA(r,l)=(ones(SN,1)'*[(exp(y_m).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))-exp(y_hat_m+(res'*res)/(nu-ka)/2).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))).^2]);
                        
                        if m<=(M-Spl)
                        f_hatagg(r,l)=[(((ones(SN,1)'*[exp(y_m).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))])/popA(1*(R+r+theta-1)+1:1*(R+r+theta)))-((ones(SN,1)'*(exp(y_hat_m+(res'*res)/(nu-ka)/2).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))))/popA(1*(R+r+theta-1)+1:1*(R+r+theta)))).^2];
                        f_hatAagg(r,l)=([(ones(SN,1)'*[exp(y_m).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))]-(ones(SN,1)'*(exp(y_hat_m+(res'*res)/(nu-ka)/2).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))))).^2]);
                        
                        f_hataggA(r,l)=([([exp(y_mA)]-exp(y_hat_mA+(resA'*resA)/(nuA-kaA)/2)).^2]);
                        f_hatAaggA(r,l)=([([exp(y_mA).*popA(1*(R+r+theta-1)+1:1*(R+r+theta))]-exp(y_hat_mA+(resA'*resA)/(nuA-kaA)/2).*popA(1*(R+r+theta-1)+1:1*(R+r+theta))).^2]);
                        end
                        end;
                    clear X1 x_time btime y_m b_n res nu ka y_hat_m y_hat_mA y_mA b_nA resA nuA kaA 
                end;
                
            end;
% 21th loop INCLUDING CRISIS DUMMYS and TIMETREND   NO STATE FIXED EFFECTS---BUT QUALITY DUMMIES 
            for q=1:QDN    
                 %waitbar(l/w)
                for m=1:MM 
                     l=l+1;
                    for r=1:N
                        Laufzeit=Laufzeit-1;
                        clc
                        disp(['Regressions left: ',num2str(Laufzeit)])
                        
                      if R+r>=32  
                        if lag==1
                            if m<=(M-Spl)
                            X1=[ones(SN*(R+r),1)  crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) crisis3(1:SN*(R+r)) QD{q}(1:SN*(R+r),:) timetrend(1:SN*(R+r)) X{m}(1:SN*(R+r),:)];
                            elseif m>(M-Spl)
                                 X1=[ones(SN*(R+r),1)  crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) crisis3(1:SN*(R+r)) QD{q}(1:SN*(R+r),:) timetrend(1:SN*(R+r)) incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag}];
                                end
                        elseif lag==2
                            if m<=(M-Spl)
                            X1=[ones(SN*(R+r),1)  crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) crisis3(1:SN*(R+r)) QD{q}(1:SN*(R+r),:) timetrend(1:SN*(R+r)) X{m}(1:SN*(R+r),:) LAGco2pc(1:SN*(R+r),:)];
                            elseif m>(M-Spl)
                            X1=[ones(SN*(R+r),1)  crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) crisis3(1:SN*(R+r)) QD{q}(1:SN*(R+r),:) timetrend(1:SN*(R+r)) incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(R+r),:)];    
                            end
                        end
                        
                     elseif R+r<32
                         
                           if lag==1
                            if m<=(M-Spl)
                            X1=[ones(SN*(R+r),1)  crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r))  QD{q}(1:SN*(R+r),:) timetrend(1:SN*(R+r)) X{m}(1:SN*(R+r),:)];
                            elseif m>(M-Spl)
                                 X1=[ones(SN*(R+r),1)  crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r))  QD{q}(1:SN*(R+r),:) timetrend(1:SN*(R+r)) incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag}];
                                end
                        elseif lag==2
                            if m<=(M-Spl)
                            X1=[ones(SN*(R+r),1)  crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r))  QD{q}(1:SN*(R+r),:) timetrend(1:SN*(R+r)) X{m}(1:SN*(R+r),:) LAGco2pc(1:SN*(R+r),:)];
                            elseif m>(M-Spl)
                            X1=[ones(SN*(R+r),1)  crisis1(1:SN*(R+r)) crisis2(1:SN*(R+r)) QD{q}(1:SN*(R+r),:) timetrend(1:SN*(R+r)) incomespline_{r,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(R+r),:)];    
                            end
                        end
                    end
                        [nu,ka]=size(X1);
                        [b{l},a,res]=regress(co2pc(1:SN*(R+r),:),X1);
                        
                     b_n=size(b{l},1);
                    if R+r>=32
                      b{l}=b{l}([1,(5):b_n]);
                    elseif R+r<32
                      b{l}=b{l}([1,(4):b_n])  ;
                    end
                    clear b_n;
                   
                        b_n=size(b{l},1);
                        q_k=size(QD{q},2);
                        
                        y_m=(co2pc(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN));
                        if lag==1
                            if m<=(M-Spl)
                            y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+timetrend(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN).*b{l}(q_k+2)+Z{m}((SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN),:)*b{l}(q_k+2+1:b_n)];
                            elseif m>(M-Spl)
                                income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(q_k+2+1:b_n);
                              y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+timetrend(SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN).*b{l}(q_k+2)+X{m}((SN*(R+r+theta-1)+1:SN*(R+r+theta-1)+SN),:).*income_b];
                                end 
                        elseif lag==2
                            if unc==1
                            if m<=(M-Spl)
                            y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+timetrend(SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN).*b{l}(q_k+2)+Z{m}((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(q_k+2+1:b_n-SN)+LAGco2pc((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                            for t=2:theta
                            y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+timetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(q_k+2)+Z{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(q_k+2+1:b_n-SN)+y_hat_m.*b{l}(b_n-SN+1:b_n)];
                            end     
                             elseif m>(M-Spl)
                                income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(q_k+2+1:b_n-SN);
                            y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+timetrend(SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN).*b{l}(q_k+2)+X{m}((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:).*income_b+LAGco2pc((SN*(R+r+1-1)+1:SN*(R+r+1-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                            for t=2:theta
                            y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+timetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(q_k+2)+X{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:).*income_b+y_hat_m.*b{l}(b_n-SN+1:b_n)];
                            end  
                            end
                            elseif unc==2
                                                        if m<=(M-Spl)
                            t=theta;
                            y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+timetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(q_k+2)+Z{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(q_k+2+1:b_n-SN)+LAGco2pc((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                                 
                             elseif m>(M-Spl)
                                income_b=income_dummy{r,(m-(M-Spl)+(10-Spl)),logs,lag}*b{l}(q_k+2+1:b_n-SN);
                            t=theta;
                            y_hat_m=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+timetrend(SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN).*b{l}(q_k+2)+X{m}((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:).*income_b+LAGco2pc((SN*(R+r+t-1)+1:SN*(R+r+t-1)+SN),:)*b{l}(b_n-SN+1:b_n)];
                                                        
                        end
                            end
                            
                        end
%----------------------------------forecasts for each model from Rr+1 bis YEARS+YEARSFOR           
if  r==N
    if lag==1
        if m<=(M-Spl)
            X1=[ones(SN*(Rr),1)  crisis1(1:SN*(Rr)) crisis2(1:SN*(Rr)) crisis3(1:SN*(Rr)) QD{q}(1:SN*(Rr),:) timetrend(1:SN*(Rr)) X{m}(1:SN*(Rr),:)];
        elseif m>(M-Spl)
            X1=[ones(SN*(Rr),1)  crisis1(1:SN*(Rr)) crisis2(1:SN*(Rr)) crisis3(1:SN*(Rr)) QD{q}(1:SN*(Rr),:) timetrend(1:SN*(Rr)) incomespline_{N+1,(m-(M-Spl)+(10-Spl)),logs,lag}];
        end
    elseif lag==2
        if m<=(M-Spl)
            X1=[ones(SN*(Rr),1)  crisis1(1:SN*(Rr)) crisis2(1:SN*(Rr)) crisis3(1:SN*(Rr)) QD{q}(1:SN*(Rr),:) timetrend(1:SN*(Rr)) X{m}(1:SN*(Rr),:) LAGco2pc(1:SN*(Rr),:)];
        elseif m>(M-Spl)
            X1=[ones(SN*(Rr),1)  crisis1(1:SN*(Rr)) crisis2(1:SN*(Rr)) crisis3(1:SN*(Rr)) QD{q}(1:SN*(Rr),:) timetrend(1:SN*(Rr)) incomespline_{N+1,(m-(M-Spl)+(10-Spl)),logs,lag} LAGco2pc(1:SN*(Rr),:)];
        end
    end

clear e n k
            [b{l},a,e,int,stat]=regress(co2pc(1:SN*(Rr),:),X1);
            [n,k]=size(X1);
            %SIC
            SIC(1,l)=[log((e'*e)/(n))+((k/n)*log(n))];
            %AIC
            AIC(1,l)=[log((e'*e)/(n))+(2*k/n)];
            %simple R2
            R_2(1,l)=stat(1,1);
            % adjusted R2
            R2adj(1,l)=1-((n-1)/(n-k))*(1-R_2(1,l));

    b_n=size(b{l},1);

    b{l}=b{l}([1,(5):b_n]);

    clear b_n;
    
    b_n=size(b{l},1);
    q_k=size(QD{q},2);

    for c=Cc:CC
        for YF=1:YEARSFOR;
            if lag==1
                if m<=(M-Spl)
                    y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+timetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(q_k+2)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(q_k+2+1:b_n)];
                elseif m>(M-Spl)
                    income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(q_k+2+1:b_n);
                    y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+timetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(q_k+2)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b];
                end
            elseif lag==2 
              if unc==1
                if m<=(M-Spl)
                    if YF==1
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+timetrend(SN*(Rr+1-1)+1:SN*(Rr+1-1)+SN).*b{l}(q_k+2)+Y{m,c}((SN*(1-1)+1:SN*(1-1)+SN),:)*b{l}(q_k+2+1:b_n-SN)+co2pc((SN*(Rr-1)+1:SN*(Rr-1)+SN)).*b{l}(b_n-SN+1:b_n)];
                    end
                    if YF>1
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+timetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(q_k+2)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(q_k+2+1:b_n-SN)+y_hat{c,l}(:,YF-1).*b{l}(b_n-SN+1:b_n)];
                    end
                elseif m>(M-Spl)
                    if YF==1
                        income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(q_k+2+1:b_n-SN);
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+timetrend(SN*(Rr+1-1)+1:SN*(Rr+1-1)+SN).*b{l}(q_k+2)+Y{m,c}((SN*(1-1)+1:SN*(1-1)+SN),:).*income_b+co2pc((SN*(Rr-1)+1:SN*(Rr-1)+SN)).*b{l}(b_n-SN+1:b_n)];
                    end
                    if YF>1
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+timetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(q_k+2)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b+y_hat{c,l}(:,YF-1).*b{l}(b_n-SN+1:b_n)];
                    end
                end
              elseif unc==2
                if m<=(M-Spl)
                    
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+timetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(q_k+2)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:)*b{l}(q_k+2+1:b_n-SN)+co2pc((SN*(Rr-1-theta+YF)+1:SN*(Rr-1-theta+YF)+SN)).*b{l}(b_n-SN+1:b_n)];
                    
                elseif m>(M-Spl)
                    
                        income_b=income_dummy{N+YF,(m-(M-Spl)+(10-Spl)),logs,c}*b{l}(q_k+2+1:b_n-SN);
                     
                        y_hat{c,l}(:,YF)=[ones(SN,1).*b{l}(1)+QD{q}(1:SN,:)*b{l}(2:q_k+1)+timetrend(SN*(Rr+YF-1)+1:SN*(Rr+YF-1)+SN).*b{l}(q_k+2)+Y{m,c}((SN*(YF-1)+1:SN*(YF-1)+SN),:).*income_b+co2pc((SN*(Rr-1-theta+YF)+1:SN*(Rr-1-theta+YF)+SN)).*b{l}(b_n-SN+1:b_n)];
                    
                end
              end
            end
         end
               if logs==2 || logs==4
               y_hat{c,l}=exp(y_hat{c,l}+(e'*e)/(n-k)/2);
               end
    end
end
%---------------------------------

                        
                        if logs==1 || logs==3 
                            f_hat(r,l)=(ones(SN,1)'*[([y_m]-y_hat_m).^2]);
                            f_hatA(r,l)=(ones(SN,1)'*[([y_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))]-y_hat_m.*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))).^2]);
                            
                        elseif logs==2 || logs==4
                            f_hat(r,l)=(ones(SN,1)'*[(exp(y_m)-exp(y_hat_m+(res'*res)/(nu-ka)/2)).^2]);
                            f_hatA(r,l)=(ones(SN,1)'*[(exp(y_m).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))-exp(y_hat_m+(res'*res)/(nu-ka)/2).*pop(SN*(R+r+theta-1)+1:SN*(R+r+theta))).^2]);
                            
                        end;
                        clear X1 x_time btime y_m b_n res nu ka 
                    end;
                    
                end;
            end;                       
            
     
        end;        %the end of the two overall loops lag and logs
       if lag==2 && unc==1
       save(halftimefilename1,'f_hat','f_hatA','b')     
       elseif lag==2 && unc==2
        save(halftimefilename2,'f_hat','f_hatA','b','y_hat')
       end
    end;
end;  
    toc
  % close(h)  
    si=size(f_hat,2);
    f_hat_diff=repmat(f_bench,1,si)-f_hat;
    f_hat_bar=mean(f_hat_diff); 
    
    %-----------------------------------------------------------------------------------------------------
    %here I will point out the best model
    diary(filename_boot);
    
    M=17;
    
    number=find(f_hat_bar==max(f_hat_bar)); %finds the position of the best alternative
    
    Number=number;
        if Number>2*ONE*(2*M+2*(M+SSpl))
       number=Number-2*ONE*(2*M+2*(M+SSpl));
       disp('from the Uncondional Loop')
        end
    
    if number>0 && number<=2*ONE*(M+SSpl)
    loop01=floor(number/(M+SSpl));
    model=number-loop01*(M+SSpl);
    loop=loop01;
    Specific=1;
    elseif number>2*ONE*(M+SSpl) && number<=2*ONE*(M+(M+SSpl))
    loop01=floor((number-2*ONE*(M+SSpl))/(M));
    model=(number-2*ONE*(M+SSpl))-loop01*(M);
    loop=loop01+2*ONE;
    Specific=2;
elseif number>2*ONE*(M+(M+SSpl)) && number<=2*ONE*(M+2*(M+SSpl))
    loop01=floor((number-2*ONE*(M+(M+SSpl)))/(M+SSpl));
    model=(number-2*ONE*(M+(M+SSpl)))-loop01*(M+SSpl);
    loop=loop01+4*ONE;
    Specific=3;
    elseif number>2*ONE*(M+2*(M+SSpl)) && number<=2*ONE*(2*M+2*(M+SSpl))
    loop01=floor((number-2*ONE*(M+2*(M+SSpl)))/(M));
    model=(number-2*ONE*(M+2*(M+SSpl)))-loop01*(M);
    loop=loop01+6*ONE;
    Specific=4;
    end
        
    if  model~=0
        loop=loop+1;    % since it gives us the finished loops --- +1 will give us the loop it is in at the time
    end;
    
if  model~=0
        loop01=loop01+1;    % since it gives us the finished loops --- +1 will give us the loop it is in at the time
    end;
   

    
   if number>0 && number<=2*ONE*(M+SSpl)
          if model==0         %since if it is the last one then it will return zero
   model=M+SSpl;
   end;
   elseif number>2*ONE*(M+SSpl) && number<=2*ONE*(M+(M+SSpl))
        if model==0         %since if it is the last one then it will return zero
        model=M;
    end;
    elseif number>2*ONE*(M+(M+SSpl)) && number<=2*ONE*(M+2*(M+SSpl))
        if model==0         %since if it is the last one then it will return zero
        model=M+SSpl;
    end;
    elseif number>2*ONE*(M+2*(M+SSpl)) && number<=2*ONE*(2*M+2*(M+SSpl))
        if model==0         %since if it is the last one then it will return zero
        model=M;
    end;
    end
    
    disp(['with an f_hat value of: ',num2str(mean(f_hat(:,number))), ' the best model has the RHS specification: ',num2str(model),])
    
    %that is the following model
    disp('that means X logs like the following');
    if model==1
        disp(' X{1}=[incomepc (incomepc.^2) (incomepc.^3) (incomepc.^4) (incomepc.^5)]');
    elseif model==2
        disp(' X{2}=[incomepc (incomepc.^2) (incomepc.^3) (incomepc.^4)]');
    elseif model==3
        disp('  X{3}=[incomepc (incomepc.^2) (incomepc.^3)]');
    elseif model==4
        disp('  X{4}=[incomepc (incomepc.^2)]');
    elseif model==5
        disp(' X{5}=[incomepc]');
    elseif model==6
        disp(' X{6}=[incomepc (incomepc.^2) (incomepc.^3) (incomepc.^4) (incomepc.^5) popdens]');
    elseif model==7
        disp('   X{7}=[incomepc (incomepc.^2) (incomepc.^3) (incomepc.^4) popdens]');
    elseif model==8
        disp('   X{8}=[incomepc (incomepc.^2) (incomepc.^3) popdens]');
    elseif model==9   
        disp('  X{9}=[incomepc (incomepc.^2) popdens]');
    elseif model==10    
        disp('   X{10}=[incomepc popdens]');
    elseif model==11  
        disp('   X{11}=[popdens]');
    elseif model==12   
        disp('   X{12}=[incomepc (incomepc.^2) (incomepc.^3) (incomepc.^4) (incomepc.^5) popdens (popdens.^2)]');
    elseif model==13  
        disp('   X{13}=[incomepc (incomepc.^2) (incomepc.^3) (incomepc.^4) popdens (popdens.^2)]');
    elseif model==14   
        disp('    X{14}=[incomepc (incomepc.^2) (incomepc.^3) popdens (popdens.^2)]');
    elseif model==15 
        disp('   X{15}=[incomepc (incomepc.^2) popdens (popdens.^2)]');
    elseif model==16   
        disp('   X{16}=[incomepc popdens (popdens.^2)]');
    elseif model==17    
        disp('   X{17}=[popdens (popdens.^2)]');
    elseif model==18   
        disp('   X{18}=3 income Splines');
    elseif model==19    
        disp('   X{19}=4 income Splines');
    elseif model==20  
        disp('   X{20}=5 income Splines');
    elseif model==21   
        disp('   X{21}=6 income Splines');
    elseif model==22   
        disp('   X{22}=7 income Splines'); 
    elseif model==23 
        disp('   X{23}=8 income Splines');
    elseif model==24  
        disp('   X{24}=9 income Splines');
    elseif model==25   
        disp('   X{25}=10 income Splines');
    end
    
    
    %first figgure out which of the big specifications (nothing, LAGs,
    %CO2-LAGS or CO2-LAGS+LAGS)
   if Specific==1
       disp('with the specification of ------ (neither LAGS or CO2-LAGS') 
   elseif Specific==2
       disp('with the specification of --LAGS--') 
   elseif Specific==3
       disp('with the specification of --CO2-LAGS--') 
   elseif Specific==4
       disp('with the specification of both --LAGS and CO2-LAGS--') 
   end;
  
   %now figgure out if it is the Levels or LOGs
   
   if loop01<=ONE
        disp('and the specification --LEVELS--') 
    elseif loop01>ONE
        disp('and the specification --LOGS--') 
    end;
    
    
  %and now we will determine which other specification in regard of the
  %State and time variables it is
  loop_form_1st=loop;
  if loop01>ONE
  loop=loop01-ONE;
  end  
    if loop>=1 && loop<=TN
        disp('with the specification:   INCLUDING State fixed effects and Time fixed effects')
    elseif  loop==TN+1
        disp('with the specification:    INCLUDING State fixed effects NO TIME FIXED EFFECTS')
    elseif  loop==TN+2
        disp('with the specification:    NO STATE FIXED EFFECTS NO TIME FIXED EFFECTS')
    elseif  loop>=TN+3 && loop<=2*TN+2
        disp('with the specification:   INCLUDING Time fixed effects NO STATE FIXED EFFECTS')
    elseif  loop==2*TN+3
        disp('with the specification:    INCLUDING State fixed effects  and TIMETREND')
    elseif  loop==2*TN+4
        disp('with the specification:   INCLUDING NO STATE FIXED EFFECTS  and TIMETREND')
    elseif  loop>=2*TN+5 && loop<=2*TN+QDN+4
        dum=loop-(2*TN+4);
        disp(['with the specification:    NO STATE FIXED EFFECTS NO TIME FIXED EFFECTS---BUT QUALITY DUMMIES combo QD-',num2str(dum)])
    elseif  loop>=2*TN+QDN+5 && loop<=2*TN+QDN+TN*QDN+4
        dum=loop-(2*TN+QDN+4);
        if dum>7
            d=floor(dum/QDN);
            dum=dum-d*7;
        end
        disp(['with the specification:   INCLUDING Time fixed effects NO STATE FIXED EFFECTS---BUT QUALITY DUMMIES  combo QD-',num2str(dum)])
    elseif  loop>=2*TN+QDN+TN*QDN+5 && loop<=2*TN+2*QDN+TN*QDN+4
        dum=loop-(2*TN+QDN+TN*QDN+4);
        disp(['with the specification:   INCLUDING NO STATE FIXED EFFECTS---BUT QUALITY DUMMIES  and TIMETREND  combo QD-',num2str(dum)])
   elseif  loop==2*TN+2*QDN+TN*QDN+5
        disp('with the specification:    INCLUDING State fixed effects  and LOG-TIMETREND')
    elseif  loop==2*TN+2*QDN+TN*QDN+6
        disp('with the specification:   INCLUDING NO STATE FIXED EFFECTS  and LOG-TIMETREND')
    elseif  loop>=2*TN+2*QDN+TN*QDN+7 && loop<=2*TN+3*QDN+TN*QDN+6
        dum=loop-(2*TN+2*QDN+TN*QDN+6);
        disp(['with the specification:   INCLUDING NO STATE FIXED EFFECTS---BUT QUALITY DUMMIES  and LOG-TIMETREND   combo QD-',num2str(dum)])
        
    elseif  loop==2*TN+3*QDN+TN*QDN+7   
        disp('with the specification:    INCLUDING State fixed effects  and CRISIS DUMMIES')
    elseif  loop==2*TN+3*QDN+TN*QDN+8
        disp('with the specification:   INCLUDING NO STATE FIXED EFFECTS  and CRISIS DUMMIES')
    elseif  loop>=2*TN+3*QDN+TN*QDN+9 && loop<=2*TN+4*QDN+TN*QDN+8
        dum=loop-(2*TN+3*QDN+TN*QDN+8);
        disp(['with the specification:   INCLUDING NO STATE FIXED EFFECTS---BUT QUALITY DUMMIES  and CRISIS DUMMIES combo QD-',num2str(dum)])
           elseif  loop==2*TN+4*QDN+TN*QDN+9
        disp('with the specification:    INCLUDING State fixed effects  and LOG-TIMETREND and CRISIS DUMMIES')
    elseif  loop==2*TN+4*QDN+TN*QDN+10
        disp('with the specification:   INCLUDING NO STATE FIXED EFFECTS  and LOG-TIMETREND and CRISIS DUMMIES')
    elseif  loop>=2*TN+4*QDN+TN*QDN+11 && loop<=2*TN+5*QDN+TN*QDN+10
        dum=loop-(2*TN+4*QDN+TN*QDN+10);
        disp(['with the specification:   INCLUDING NO STATE FIXED EFFECTS---BUT QUALITY DUMMIES  and LOG-TIMETREND and CRISIS DUMMIES combo QD-',num2str(dum)])
           elseif  loop==2*TN+5*QDN+TN*QDN+11
        disp('with the specification:    INCLUDING State fixed effects  and TIMETREND and CRISIS DUMMIES')
    elseif  loop==2*TN+5*QDN+TN*QDN+12
        disp('with the specification:   INCLUDING NO STATE FIXED EFFECTS  and TIMETREND and CRISIS DUMMIES')
    elseif  loop>=2*TN+5*QDN+TN*QDN+13 && loop<=2*TN+6*QDN+TN*QDN+12
        dum=loop-(2*TN+5*QDN+TN*QDN+12);
        disp(['with the specification:   INCLUDING NO STATE FIXED EFFECTS---BUT QUALITY DUMMIES  and TIMETREND and CRISIS DUMMIES combo QD-',num2str(dum)])
    end     

    
    disp(['loop ',num2str(loop_form_1st), ' of ',num2str(8*ONE)])
        disp(['and number ',num2str(Number), ' of ',num2str(l)])
    disp(['The Program needed ', num2str(toc/60),' minuts for the model regressions'])
    
   %------------------------------------------------------------------------------------------------------------
  disp('') 
  disp('----------------------------------------------------------------------') 
  disp('')
  disp('Now we look for the selection criterion based on the aggregated CO2 emissions')
   
   
   %now for the f_hatA's that is the aggregated selection criteria MSFE 
    si=size(f_hatA,2);
    f_hat_diffA=repmat(f_benchA,1,si)-f_hatA;
    f_hat_barA=mean(f_hat_diffA); 
    
    %-----------------------------------------------------------------------------------------------------
    %here I will point out the best model
    numberA=find(f_hat_barA==max(f_hat_barA)); %finds the position of the best alternative
   
    
     NumberA=numberA;
        if NumberA>2*ONE*(2*M+2*(M+SSpl))
       numberA=NumberA-2*ONE*(2*M+2*(M+SSpl));
       disp('the best model is from the Uncondional Loop')
        end
    disp('')
    if numberA>0 && numberA<=2*ONE*(M+SSpl)
    loop01=floor(numberA/(M+SSpl));
    model=numberA-loop01*(M+SSpl);
    loop=loop01;
    Specific=1;
    elseif numberA>2*ONE*(M+SSpl) && numberA<=2*ONE*(M+(M+SSpl))
    loop01=floor((numberA-2*ONE*(M+SSpl))/(M));
    model=(numberA-2*ONE*(M+SSpl))-loop01*(M);
    loop=loop01+2*ONE;
    Specific=2;
elseif numberA>2*ONE*(M+(M+SSpl)) && numberA<=2*ONE*(M+2*(M+SSpl))
    loop01=floor((numberA-2*ONE*(M+(M+SSpl)))/(M+SSpl));
    model=(numberA-2*ONE*(M+(M+SSpl)))-loop01*(M+SSpl);
    loop=loop01+4*ONE;
    Specific=3;
    elseif numberA>2*ONE*(M+2*(M+SSpl)) && numberA<=2*ONE*(2*M+2*(M+SSpl))
    loop01=floor((numberA-2*ONE*(M+2*(M+SSpl)))/(M));
    model=(numberA-2*ONE*(M+2*(M+SSpl)))-loop01*(M);
    loop=loop01+6*ONE;
    Specific=4;
    end
        
    if  model~=0
        loop=loop+1;    % since it gives us the finished loops --- +1 will give us the loop it is in at the time
    end;
    
   if numberA>0 && numberA<=2*ONE*(M+SSpl)
   if model==0         %since if it is the last one then it will return zero
   model=M+SSpl;
   end;
   elseif numberA>2*ONE*(M+SSpl) && numberA<=2*ONE*(M+(M+SSpl))
        if model==0         %since if it is the last one then it will return zero
        model=M;
    end;
    elseif numberA>2*ONE*(M+(M+SSpl)) && numberA<=2*ONE*(M+2*(M+SSpl))
        if model==0         %since if it is the last one then it will return zero
        model=M+SSpl;
    end;
    elseif numberA>2*ONE*(M+2*(M+SSpl)) && numberA<=2*ONE*(2*M+2*(M+SSpl))
        if model==0         %since if it is the last one then it will return zero
        model=M;
    end;
  
    end
    
    disp(['with an f_hat value of: ',num2str(mean(f_hatA(:,NumberA))), ' the best model has the RHS specification: ',num2str(model),])
    
    %that is the following model
    disp('that means X logs like the following');
    if model==1
        disp(' X{1}=[incomepc (incomepc.^2) (incomepc.^3) (incomepc.^4) (incomepc.^5)]');
    elseif model==2
        disp(' X{2}=[incomepc (incomepc.^2) (incomepc.^3) (incomepc.^4)]');
    elseif model==3
        disp('  X{3}=[incomepc (incomepc.^2) (incomepc.^3)]');
    elseif model==4
        disp('  X{4}=[incomepc (incomepc.^2)]');
    elseif model==5
        disp(' X{5}=[incomepc]');
    elseif model==6
        disp(' X{6}=[incomepc (incomepc.^2) (incomepc.^3) (incomepc.^4) (incomepc.^5) popdens]');
    elseif model==7
        disp('   X{7}=[incomepc (incomepc.^2) (incomepc.^3) (incomepc.^4) popdens]');
    elseif model==8
        disp('   X{8}=[incomepc (incomepc.^2) (incomepc.^3) popdens]');
    elseif model==9   
        disp('  X{9}=[incomepc (incomepc.^2) popdens]');
    elseif model==10    
        disp('   X{10}=[incomepc popdens]');
    elseif model==11  
        disp('   X{11}=[popdens]');
    elseif model==12   
        disp('   X{12}=[incomepc (incomepc.^2) (incomepc.^3) (incomepc.^4) (incomepc.^5) popdens (popdens.^2)]');
    elseif model==13  
        disp('   X{13}=[incomepc (incomepc.^2) (incomepc.^3) (incomepc.^4) popdens (popdens.^2)]');
    elseif model==14   
        disp('    X{14}=[incomepc (incomepc.^2) (incomepc.^3) popdens (popdens.^2)]');
    elseif model==15 
        disp('   X{15}=[incomepc (incomepc.^2) popdens (popdens.^2)]');
    elseif model==16   
        disp('   X{16}=[incomepc popdens (popdens.^2)]');
    elseif model==17    
        disp('   X{17}=[popdens (popdens.^2)]');
    elseif model==18   
        disp('   X{18}=3 income Splines');
    elseif model==19    
        disp('   X{19}=4 income Splines');
    elseif model==20  
        disp('   X{20}=5 income Splines');
    elseif model==21   
        disp('   X{21}=6 income Splines');
    elseif model==22   
        disp('   X{22}=7 income Splines'); 
    elseif model==23 
        disp('   X{23}=8 income Splines');
    elseif model==24  
        disp('   X{24}=9 income Splines');
    elseif model==25   
        disp('   X{25}=10 income Splines');
    end
    
    
    %first figgure out which of the big specifications (nothing, LAGs,
    %CO2-LAGS or CO2-LAGS+LAGS)
   if Specific==1
       disp('with the specification of ------ (neither LAGS or CO2-LAGS') 
   elseif Specific==2
       disp('with the specification of --LAGS--') 
   elseif Specific==3
       disp('with the specification of --CO2-LAGS--') 
   elseif Specific==4
       disp('with the specification of both --LAGS and CO2-LAGS--') 
   end;
  
   %now figgure out if it is the Levels or LOGs
   
   if loop01<=ONE
        disp('and the specification --LEVELS--') 
    elseif loop01>ONE
        disp('and the specification --LOGS--') 
    end;
    
    
  %and now we will determine which other specification in regard of the
  %State and time variables it is
  loop_form_2nd=loop;
  if loop01>ONE
  loop=loop01-ONE;  
  end
    if loop>=1 && loop<=TN
        disp('with the specification:  LEVELS INCLUDING State fixed effects and Time fixed effects')
    elseif  loop==TN+1
        disp('with the specification:  LEVELS  INCLUDING State fixed effects NO TIME FIXED EFFECTS')
    elseif  loop==TN+2
        disp('with the specification:  LEVELS  NO STATE FIXED EFFECTS NO TIME FIXED EFFECTS')
    elseif  loop>=TN+3 && loop<=2*TN+2
        disp('with the specification:  LEVELS INCLUDING Time fixed effects NO STATE FIXED EFFECTS')
    elseif  loop==2*TN+3
        disp('with the specification:  LEVELS  INCLUDING State fixed effects  and TIMETREND')
    elseif  loop==2*TN+4
        disp('with the specification:  LEVELS INCLUDING NO STATE FIXED EFFECTS  and TIMETREND')
    elseif  loop>=2*TN+5 && loop<=2*TN+QDN+4
        dum=loop-(2*TN+4);
        disp(['with the specification:  LEVELS  NO STATE FIXED EFFECTS NO TIME FIXED EFFECTS---BUT QUALITY DUMMIES combo QD-',num2str(dum)])
    elseif  loop>=2*TN+QDN+5 && loop<=2*TN+QDN+TN*QDN+4
        dum=loop-(2*TN+QDN+4);
        if dum>7
            d=floor(dum/QDN);
            dum=dum-d*7;
        end
        disp(['with the specification:  LEVELS INCLUDING Time fixed effects NO STATE FIXED EFFECTS---BUT QUALITY DUMMIES  combo QD-',num2str(dum)])
    elseif  loop>=2*TN+QDN+TN*QDN+5 && loop<=2*TN+2*QDN+TN*QDN+4
        dum=loop-(2*TN+QDN+TN*QDN+4);
        disp(['with the specification:  LEVELS INCLUDING NO STATE FIXED EFFECTS---BUT QUALITY DUMMIES  and TIMETREND  combo QD-',num2str(dum)])
   elseif  loop==2*TN+2*QDN+TN*QDN+5
        disp('with the specification:  LEVELS  INCLUDING State fixed effects  and LOG-TIMETREND')
    elseif  loop==2*TN+2*QDN+TN*QDN+6
        disp('with the specification:  LEVELS INCLUDING NO STATE FIXED EFFECTS  and LOG-TIMETREND')
    elseif  loop>=2*TN+2*QDN+TN*QDN+7 && loop<=2*TN+3*QDN+TN*QDN+6
        dum=loop-(2*TN+2*QDN+TN*QDN+6);
        disp(['with the specification:  LEVELS INCLUDING NO STATE FIXED EFFECTS---BUT QUALITY DUMMIES  and LOG-TIMETREND   combo QD-',num2str(dum)])
        
    elseif  loop==2*TN+3*QDN+TN*QDN+7   
        disp('with the specification:  LEVELS  INCLUDING State fixed effects  and CRISIS DUMMIES')
    elseif  loop==2*TN+3*QDN+TN*QDN+8
        disp('with the specification:  LEVELS INCLUDING NO STATE FIXED EFFECTS  and CRISIS DUMMIES')
    elseif  loop>=2*TN+3*QDN+TN*QDN+9 && loop<=2*TN+4*QDN+TN*QDN+8
        dum=loop-(2*TN+3*QDN+TN*QDN+8);
        disp(['with the specification:  LEVELS INCLUDING NO STATE FIXED EFFECTS---BUT QUALITY DUMMIES  and CRISIS DUMMIES combo QD-',num2str(dum)])
           elseif  loop==2*TN+4*QDN+TN*QDN+9
        disp('with the specification:  LEVELS  INCLUDING State fixed effects  and LOG-TIMETREND and CRISIS DUMMIES')
    elseif  loop==2*TN+4*QDN+TN*QDN+10
        disp('with the specification:  LEVELS INCLUDING NO STATE FIXED EFFECTS  and LOG-TIMETREND and CRISIS DUMMIES')
    elseif  loop>=2*TN+4*QDN+TN*QDN+11 && loop<=2*TN+5*QDN+TN*QDN+10
        dum=loop-(2*TN+4*QDN+TN*QDN+10);
        disp(['with the specification:  LEVELS INCLUDING NO STATE FIXED EFFECTS---BUT QUALITY DUMMIES  and LOG-TIMETREND and CRISIS DUMMIES combo QD-',num2str(dum)])
           elseif  loop==2*TN+5*QDN+TN*QDN+11
        disp('with the specification:  LEVELS  INCLUDING State fixed effects  and TIMETREND and CRISIS DUMMIES')
    elseif  loop==2*TN+5*QDN+TN*QDN+12
        disp('with the specification:  LEVELS INCLUDING NO STATE FIXED EFFECTS  and TIMETREND and CRISIS DUMMIES')
    elseif  loop>=2*TN+5*QDN+TN*QDN+13 && loop<=2*TN+6*QDN+TN*QDN+12
        dum=loop-(2*TN+5*QDN+TN*QDN+12);
        disp(['with the specification:  LEVELS INCLUDING NO STATE FIXED EFFECTS---BUT QUALITY DUMMIES  and TIMETREND and CRISIS DUMMIES combo QD-',num2str(dum)])
    end    
    
        disp(['loop ',num2str(loop_form_2nd), ' of ',num2str(8*ONE)])
        disp(['and number ',num2str(NumberA), ' of ',num2str(l)])
    disp(['The Program needed ', num2str(toc/60),' minuts for the model regressions'])
%------------------------------------------------------------------------------------------------------------

%------------------------------------------------------------------------------------------------------------
    %here comes the bootstrap as last specification here the block lenght
    %need to be specified

    
block=5; 

%will save necessary boostarp input datsa
save(filenameBoot,'f_hat' ,'f_hatA','f_bench' ,'f_B2b' ,'f_B3' ,'f_benchA' ,'f_B2bA' ,'f_B3A' ,'block','Number','NumberA');

disp('Schmalensee')
 [Trc,Tspc,u,u_10percent,c_hansen,c_hansen_10percent,naive_p,naive_10percent,best,besthansen]=bsds_ralf_ess(f_bench,f_hat,500,block,'STATIONARY',Number)

%Holtz_Eakin Selden
disp('Holtz_Eakin Selden')
[Trc,Tspc,u,u_10percent,c_hansen,c_hansen_10percent,naive_p,naive_10percent,best,besthansen]=bsds_ralf_ess(f_B2b,f_hat,500,block,'STATIONARY',Number)

%Yang and Schneider
disp('Yang and Schneider')
[Trc,Tspc,u,u_10percent,c_hansen,c_hansen_10percent,naive_p,naive_10percent,best,besthansen]=bsds_ralf_ess(f_B3,f_hat,500,block,'STATIONARY',Number)

%now for the Aggregates group
disp('Now the aggregated criterion Bootstrap test--Schmalensee')
[Trc,Tspc,u,u_10percent,c_hansen,c_hansen_10percent,naive_p,naive_10percent,best,besthansen]=bsds_ralf_ess(f_benchA,f_hatA,500,block,'STATIONARY',NumberA)
   
%Holtz_Eakin Selden
disp('Now the aggregated criterion Bootstrap test--Holtz_Eakin Selden')
[Trc,Tspc,u,u_10percent,c_hansen,c_hansen_10percent,naive_p,naive_10percent,best,besthansen]=bsds_ralf_ess(f_B2bA,f_hatA,500,block,'STATIONARY',NumberA)

%Yang and schneider
disp('Now the aggregated criterion Bootstrap test--Yang and Schneider')
[Trc,Tspc,u,u_10percent,c_hansen,c_hansen_10percent,naive_p,naive_10percent,best,besthansen]=bsds_ralf_ess(f_B3A,f_hatA,500,block,'STATIONARY',NumberA)





diary off



